我有一个集合:
public static readonly DependencyProperty UserAutoCalculationTemplatesProperty = DependencyProperty.Register("UserAutoCalculationTemplates", typeof(ObservableCollection<AutoCalculationTemplate>), typeof(Options));
public ObservableCollection<AutoCalculationTemplate> UserAutoCalculationTemplates
{
get => (ObservableCollection<AutoCalculationTemplate>)GetValue(UserAutoCalculationTemplatesProperty);
set => SetValue(UserAutoCalculationTemplatesProperty, value);
}
AutoCalculationTemplate类:
public class AutoCalculationTemplate : INotifyPropertyChanged
{
private CalculationVariants _templateType;
public CalculationVariants TemplateType
{
get
{
return _templateType;
}
set
{
_templateType = value;
OnPropertyChanged(new PropertyChangedEventArgs("TemplateType"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
}
我的XAML。
<DataGrid ItemsSource="{Binding UserAutoCalculationTemplates, ElementName=OptionsWindow,Mode=TwoWay,NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Name="AutoCalculaionTemplates">
<DataGrid.Columns>
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
x:Name="AutoCalculationTypeCombo"
ItemsSource="{Binding Source={enum:Enumeration {x:Type options:CalculationVariants}}}"
DisplayMemberPath="Description"
SelectedValue="{Binding UserAutoCalculationTemplates.TemplateType, ElementName=OptionsWindow,Mode=TwoWay,NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Value"
DropDownClosed="AutoCalculationTypeCombo_OnDropDownClosed"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGrid.Columns>
</DataGrid>
通过此主题的扩展绑定到枚举的组合框:将枚举属性数据绑定到WPF:中的组合框
enum:
public enum CalculationVariants
{
[Description("Внутренний")]
CalculateWithCSharp,
[Description("Внешний скрипт")]
CalculateWithPython,
[Description("Общий")]
Common
}
我想要Combobox做两件事:
- 点击时显示枚举的所有可能值(它们的描述(
- 显示UserAutoCalculationTemplatesProperty集合中项的TemplateType属性的当前值(空闲(
目前它只做第一件事,而不做第二件事,我该如何修复它?
以下示例将ComboBox.SelectedValue
绑定到OptionsWindow
的属性:
public static readonly DependencyProperty SelectedCalculationVariantsProperty = DependencyProperty.Register(
"SelectedCalculationVariants",
typeof(CalculationVariants),
typeof(Options));
public CalculationVariants SelectedCalculationVariants
{
get => (CalculationVariants) GetValue(SelectedCalculationVariantsProperty);
set => SetValue(SelectedCalculationVariantsProperty, value);
}
<DataGrid.Columns>
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="AutoCalculationTypeCombo"
ItemsSource="{Binding Source={enum:Enumeration {x:Type options:CalculationVariants}}}"
DisplayMemberPath="Description"
SelectedValue="{Binding SelectedCalculationVariants, ElementName=OptionsWindow, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Value"
DropDownClosed="AutoCalculationTypeCombo_OnDropDownClosed"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGrid.Columns>