DataGrid的DataGridComboBoxColumn绑定到Dictionary

时间:2013-12-16 16:42:38

标签: c# wpf xaml datagrid combobox

我是WPF的新手,我尝试让DataGrid的DataGridComboBoxColumn正确设置绑定。

我有一个组合框列的可能元素字典,Key是item属性应该具有的Id,Value是要在ComboBox中显示的文本。字典看起来像这样:

public static Dictionary<int, string> users;

和填充DataGrid的项目列表,每个项目都有一个组合框的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绑定以获取用户Dictionary的值,并将值选择为OwnerId属性的值。

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

3 个答案:

答案 0 :(得分: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;

}
  

的FileItem

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;

}

答案 1 :(得分:1)

您的DataGridComboBoxColumn需要绑定到词典。这意味着您需要将ItemsSource设置为它。执行此操作后,您的DisplayMemberPath可能是您的词典项目的Value和SelectedValuePath,而您的词典项目是KeyDictionary在内部将所有内容存储为具有KeyValuePair<TKey, TValue>Key属性的Value

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

SelectedValuePath="Key" DisplayMemberPath="Value"

答案 2 :(得分:0)

我还补充说“Key”和“Value”区分大小写。 在我的代码中,

SelectedValuePath="key"

不起作用,组合框显示空项目,即使它与字典长度一样多。