【问题标题】:WPF: DataGridComboBoxColumn styling ComboBoxItemWPF:DataGridComboBoxColumn 样式 ComboBoxItem
【发布时间】:2015-03-27 16:13:20
【问题描述】:

我在<UserControl.Resources>中定义了这种风格:

<Style x:Name="DropDownStyle" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="5,2,2,2" Text="{Binding ID, Mode=OneWay}" />
                    <TextBlock Margin="5,2,10,2" Text="{Binding Name, Mode=OneWay}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用于将ComboBoxItem的项目显示为1 Car2 Truck

我想知道如何在 DataGridComboBoxColumn 项目中移动此样式,因为此样式已应用于我的所有其他 DataGridComboBoxColumn

我不知道如何深入到DataGridComboBoxColumnComboBoxItem 元素来应用样式。

【问题讨论】:

  • 我猜您只想将该样式应用于该特定 DataGrid 中的 ComboBoxes。在这种情况下,删除样式的x:Name 并将样式移动到DataGrid &lt;DataGrid.Resources&gt; 的资源中。缺点是它会更改所有列中的所有 ComboBox。
  • @gomi42 感谢您的建议,但其他 DataGridComboBoxColumns 在同一个 DataGrid 中,因此在我的情况下,在 DataGrid 中移动样式不起作用。

标签: c# wpf datagridcomboboxcolumn


【解决方案1】:

我明白了。您可以通过以下方式完成您想要的。将现有的ComboBoxItem 样式保留在原处。为 Combobox 添加另一种样式,在下面的示例中,它是 DataGridComboBoxColumn 中的 MyComboBoxStyle´ and refore to theEditingElementStyle´。

<Style x:Key="MyComboboxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemContainerStyle" Value="{StaticResource DropDownStyle}"/>
</Style>


<DataGridComboBoxColumn EditingElementStyle="{StaticResource MyComboboxStyle}" SelectedItemBinding="{Binding MySelectedItem}" ItemsSource="{StaticResource MyArray}">

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2017-02-10
    相关资源
    最近更新 更多