WPF以编程方式创建组合框

时间:2018-03-19 08:49:33

标签: c# .net wpf

我试图从后面的代码创建DatagridComboBoxColumn。如何将其与ElementStyleEditingElementStyle

相关联

我试图创建这个:

<DataGridComboBoxColumn Width="*" ElementStyle="{StaticResource ComboBoxElementStyle}" EditingElementStyle="{StaticResource ComboBoxEditingElementStyle}" />

创建了这个: enter image description here

来自:

private DataGridComboBoxColumn CreateComboValueColumn()
{
    DataGridComboBoxColumn column = new DataGridComboBoxColumn();
    column.ElementStyle = ???;
    column.EditingElementStyle = ???;
    return column;
}

哪个不显示comboBox:

enter image description here

我试图链接的风格:

<!-- ComboBox element and editing style -->
<Style x:Key="ComboBoxElementStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemsSource" Value="{Binding ComboItems}"/>
    <Setter Property="SelectedValue" Value="{Binding Value}" />
    <Setter Property="DisplayMemberPath" Value="Text"/>
    <Setter Property="SelectedValuePath" Value="ID" />
</Style>
<Style x:Key="ComboBoxEditingElementStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemsSource" Value="{Binding ComboItems}"/>
    <Setter Property="SelectedValue" Value="{Binding Value}" />
    <Setter Property="DisplayMemberPath" Value="Text"/>
    <Setter Property="SelectedValuePath" Value="ID" />
</Style>

2 个答案:

答案 0 :(得分:1)

你好试试这样的事情:

private DataGridComboBoxColumn CreateComboValueColumn()
{
DataGridComboBoxColumn column = new DataGridComboBoxColumn();
column.ElementStyle = YourWindowName.FindResource("ComboBoxElementStyle") as Style;
column.EditingElementStyle = YourWindowName.FindResource("ComboBoxEditingElementStyle") as Style;
return column;
}

答案 1 :(得分:0)

尝试(Style)FindResource("StyleOne")

之类的内容

看看这里: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/resources-and-code