我是 WPF 的新手,我尝试让 DataGrid 的 DataGridComboBoxColumn 正确设置绑定(bind)。
我有一个包含组合框列可能元素的字典,键是项目属性应具有的 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 绑定(bind)以获取用户字典的值,并将该值选择为 OwnerId 属性的值。
PS:我不确定 DataContext 是否应该像现在一样具有值“{Binding FileItems}”。
最佳答案
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();
}
}
ViewModel
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;
}
关于c# - DataGrid 的 DataGridComboBoxColumn 绑定(bind)到 Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20616070/