[英]Binding Dictionary<string, int> to
This is my Enum 这是我的枚举
public enum Works
{
[Description("Admin")]
Admin,
[Description("Chưa Set")]
UnSetted,
[Description("Vào - Ra")]
InOut,
[Description("Ra - Vào")]
OutIn
}
This is my ViewModel 这是我的ViewModel
public class DivisionViewModel : ViewModel<IDivisionView>
{
private IEnumerable<Account> accounts;
private ICommand confirmCommand;
private Dictionary<string, int> works;
....
}
I convert the enum to get a Dictionary and get accounts like this 我将枚举转换为字典并获得这样的帐户
this.divisionViewModel.Accounts = Root.Accounts;
this.divisionViewModel.Works = Enum.GetValues(typeof(Works))
.Cast<Works>()
.ToDictionary(t => Tools.GetEnumDescription((Works)t), t => (int)t);
Here is my WPF DataGrid 这是我的WPF DataGrid
<DataGrid ItemsSource="{Binding Accounts}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding FullName, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
Header="Tên nhân viên" Width="*" ElementStyle="{StaticResource TextCellElementStyle}"
EditingElementStyle="{StaticResource TextCellEditingStyle}"/>
<DataGridComboBoxColumn></DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
How can I bind the this.divisionViewModel.Works
to the DataGridComboBoxColumn
? 如何将this.divisionViewModel.Works
绑定到DataGridComboBoxColumn
? p/s: I want my user to change the Work directly on the DataGrid. p / s:我希望我的用户直接在DataGrid上更改Work。 I have seachred on SO. 我对SO非常着迷。 But nobody seems to talk about binding Dictionary to a DataGridComboBoxColumn. 但是似乎没有人谈论将Dictionary绑定到DataGridComboBoxColumn。
The reason why this is empty: 这是空的原因:
<DataGridComboBoxColumn ItemsSource="{Binding Works.Keys}">
is because the ComboBox
will have the current Account
bound as a ViewModel. 这是因为ComboBox
将当前Account
绑定为ViewModel。 You need to bind to the DataGrid
's ViewModel, and that's where RelativeSource
comes in handy: 您需要绑定到DataGrid
的ViewModel,这就是RelativeSource
派上用场的地方:
<DataGridComboBoxColumn ItemsSource="{Binding Works.Keys, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
Note: Check your Output Window in Visual Studio, you'll see Binding errors telling you that they can't find a Property named Work
in an Account
注意:在Visual Studio中检查“输出”窗口,您会看到绑定错误,告诉您它们在Account
找不到名为Work
的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.