C#WPF-DataGridComboBoxColumn ItemsSource

我目前在C#WPF中使用DataGridComboBoxColumn挣扎.

我有ToolModel类

class ToolModel
{
    public long Id { get; set; }
    public string Number { get; set; }
    public string Name { get; set; }
    public string Coating { get; set; }
    public bool Thread { get; set; }
    public string Kind { get; set; }
    public ToolTypeModel Type { get; set; }
}

和类ToolTypeModel

public class ToolTypeModel
{
    public long Id { get; set; }
    public string Name { get; set; }
}

数据存储到数据库中并加载到我的ViewModel中

class ToolsViewModel
{
    public ObservableCollection<ToolModel> Tools { get; set; }
    public ObservableCollection<ToolTypeModel> ToolTypes { get; set; }

    public ToolsViewModel()
    {
        Tools = new ObservableCollection<ToolModel>(ToolModel.GetTools());
        ToolTypes = new ObservableCollection<ToolTypeModel>(ToolTypeModel.GetToolTypes());
    }
}

我想在DataGrid中显示数据,并尝试添加DataGridComboBoxColumn为对应的工具选择ToolType.因此,我添加了以下xaml定义(这已经是一种解决方法-但只有这样我才能使它接近工作):

 <DataGrid x:Name="ToolsDataGrid" ItemsSource="{Binding Tools}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Nummer" Binding="{Binding Number}" />
            <DataGridComboBoxColumn 
                    Header="Typ"
                    SelectedValueBinding="{Binding Type, Mode=TwoWay}" 
                    SelectedValuePath="Id"
                    DisplayMemberPath="Name">

                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ToolTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ToolTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

它显示了ToolTypes的所有元素,但没有显示我的Tool所引用的所选元素.

有什么想法可以将ToolTypes查找到ComboBoxColumn中,并将引用的Type显示为选定项吗?

提前致谢.

解决方法:

SelectedValuePath是将通过SelectedValueBinding绑定的组合框项目的属性的路径.因此,SelectedValueBinding必须绑定到相同类型的属性.

<DataGridComboBoxColumn 
    Header="Typ"
    SelectedValueBinding="{Binding Type, Mode=TwoWay}" 
    SelectedValuePath="Id"
    DisplayMemberPath="Name">

如果您具有ToolTypeId属性,则可以使用SelectedValuePath绑定到该属性:

class ToolModel
{
    public long Id { get; set; }
    public string Number { get; set; }
    public string Name { get; set; }
    public string Coating { get; set; }
    public bool Thread { get; set; }
    public string Kind { get; set; }

    //  Like so
    public long ToolTypeId { get; set; }

    public ToolTypeModel Type { get; set; }
}

在XAML中:

<DataGridComboBoxColumn 
    Header="Typ"
    SelectedValueBinding="{Binding ToolTypeId}" 
    SelectedValuePath="Id"
    DisplayMemberPath="Name">

但是我认为那不是您想要的.我想很明显,您想在Type属性中获取实际的ToolTypeModel实例.

所以这应该工作(我刚刚测试过).但是,请小心:重写Equals()有点粗略,因为它会更改该类的C#=和!=运算符的语义,这可能会咬人.

public class ToolTypeModel
{
    public long Id { get; set; }
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        return (obj is ToolTypeModel)
                    ? (obj as ToolTypeModel).Id == Id
                    : false;
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
}

XAML:

<DataGridComboBoxColumn
    Header="Type"
    SelectedItemBinding="{Binding Type}" 
    DisplayMemberPath="Name"
    >

(此外,摆脱Mode = TwoWay,这是该属性上绑定的默认设置,无论如何您都会得到它).

上一篇:c#-使用数据绑定访问DataTemplate内部的WPF组件


下一篇:应用中的C#WPF多个面板-如何做到这一点