DataGrid的DataGridComboBoxColumn如何绑定到字典?

3

我是WPF的新手,尝试设置DataGridComboBoxColumn的绑定。

我有一个可能元素的字典用于组合框列,键是项属性应该具有的Id,值是ComboBox中显示的文本。 字典如下:

public static Dictionary<int, string> users;

数据网格中有一个项目列表,每个项目都有一个Id值用于下拉列表:

public static List<FileItem> fileItems = new List<FileItem>();

//...

public class FileItem {
    public int OwnerId { get; set; }
    //...
}

XAML现在看起来是这样的:
<DataGrid x:Name="DataGridUpdates" Margin="12,74,10,313" AutoGenerateColumns="False" DataContext="{Binding FileItems}">
<DataGrid.Columns>
    <DataGridComboBoxColumn  x:Name="ClmOwner" Header="Owner" ClipboardContentBinding="{x:Null}"  SelectedValueBinding="{x:Null}" SelectedItemBinding="{x:Null}" TextBinding="{x:Null}"/>
</DataGrid.Columns>
</DataGrid>

我尝试使用以下方法:

SelectedValueBinding="{Binding Path=OwnerId}" SelectedValuePath="OwnerId"

但是它没有起作用,行中显示了空的ComboBox,因为它没有ItemsSource,我不知道在哪里设置它。

在代码后台中,我可以像这样设置ItemsSource,以至少设置值列表:

ClmOwner.ItemsSource = FileItem.users;

但我更喜欢使用XAML。

问题是如何设置ComboBox的XAML绑定,以获取用户字典的值,并将值选择为OwnerId属性的值。

PS:我不确定DataContext是否应该像现在这样使用值"{Binding FileItems}"。

3个回答

2
您的 DataGridComboBoxColumn 需要绑定到字典。这意味着您需要将 ItemsSource 设置为它。一旦您这样做了,您的 DisplayMemberPath 很可能是您的字典项的 Value,而 SelectedValuePath 将是您的字典项的 KeyDictionary 在内部将所有内容存储为 KeyValuePair<TKey, TValue>,其中包含 KeyValue 属性。

ItemSource 绑定到字典的实例并尝试:

SelectedValuePath="Key" DisplayMemberPath="Value"

谢谢,这两个xaml属性正是我所需要的,如果我在代码中设置ItemsSource,那就可以了。 - user3009724

2

xaml

<DataGrid x:Name="DataGridUpdates" Margin="12,74,10,313" AutoGenerateColumns="False" CanUserAddRows="False"
              ItemsSource="{Binding FileItems}" 
              SelectedValue="{Binding SelectedOwnerId, Mode=TwoWay}" SelectedValuePath="Key"
              >
        <DataGrid.Columns>
            <DataGridComboBoxColumn  x:Name="ClmOwner" Header="Owner"
                   ItemsSource="{Binding Source={x:Static local:MyViewModel.Users}, Mode=OneWay}"   DisplayMemberPath="Value"
                   SelectedItemBinding="{Binding ComboSelectedItem}"
                   />
        </DataGrid.Columns>
    </DataGrid>

xaml.cs

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MyViewModel();
    }
}

视图模型

public class MyViewModel : INotifyPropertyChanged
{
    static Dictionary<int, string> users;
    //Lets say this is ur static dictionary
    public static Dictionary<int, string> Users
    {
        get
        {
            return users ?? (users = new Dictionary<int, string> { 
            {1,"User1"},
            {2,"User2"},
            {3,"User3"}
            });
        }
    }

    public MyViewModel()
    {
        //Fill the collection
        FileItems = new ObservableCollection<FileItem>
            {
            new FileItem{OwnerId=1},
            new FileItem{OwnerId=2},
            new FileItem{OwnerId=3},
            };

    }

    //This will be binded to the ItemSource of DataGrid
    public ObservableCollection<FileItem> FileItems { get; set; }

    //Selected Owner Id . Notify if TwoMode binding required
    int selectedOwnerId;
    public int SelectedOwnerId
    {
        get
        { return selectedOwnerId; }
        set { selectedOwnerId = value; Notify("SelectedOwnerId"); }
    }

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}
文件项
public class FileItem : INotifyPropertyChanged
{
    int ownerId;
    public int OwnerId
    {
        get
        { return ownerId; }
        set { ownerId = value; Notify("OwnerId"); }
    }

    KeyValuePair<int, string> comboSelectedItem;
    //This will have ComboBox Selected Item If SO need it 
    public KeyValuePair<int, string> ComboSelectedItem
    {
        get { return comboSelectedItem; }
        set { comboSelectedItem = value; Notify("ComboSelectedItem"); }
    }

    //.... other properties
    //.....

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

0
我还要补充一点,"Key"和"Value"是区分大小写的。在我的代码中,
SelectedValuePath="key"

不起作用,即使字典长度与下拉框中的项目数量相同,下拉框仍显示为空。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接