【问题标题】:DataGrid's DataGridComboBoxColumn binding to DictionaryDataGrid 的 DataGridComboBoxColumn 绑定到 Dictionary
【发布时间】:2014-01-04 03:32:45
【问题描述】:

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

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

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 绑定以获取用户字典的值,并将值选择为 OwnerId 属性的值。

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

【问题讨论】:

    标签: c# wpf xaml datagrid combobox


    【解决方案1】:

    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;
    
    }
    

    【讨论】:

      【解决方案2】:

      您的DataGridComboBoxColumn 需要绑定到字典。这意味着您需要将ItemsSource 设置为它。完成此操作后,您的 DisplayMemberPath 可能是您的 Dictionary 项目的 Value 和 SelectedValuePath 以及您的 Dictionary 项目的 KeyDictionary 在内部将所有内容存储为具有 KeyValue 属性的 KeyValuePair&lt;TKey, TValue&gt;

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

      SelectedValuePath="Key" DisplayMemberPath="Value"
      

      【讨论】:

      • 谢谢,这两个 xaml 属性正是我所需要的,如果我在代码隐藏中设置 ItemsSource 就可以了。
      【解决方案3】:

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

      SelectedValuePath="key"
      

      不起作用,即使有与字典长度一样多的组合框也会显示空项。

      【讨论】:

        猜你喜欢
        • 2015-01-21
        • 1970-01-01
        • 2012-12-19
        • 2017-08-05
        • 2013-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多