我有一个带有此定义的自定义ComboBox。
<UserControl ...>
<Grid>
<Grid.Resources>
<local:InheritanceEnumValueConverter x:Key="inheritanceEnumValueConverter"/>
</Grid.Resources>
<samples:MaterialEnumComboBox x:Name="materialComboBoxGenerateStatements"
Grid.Column="3" Grid.Row="1" Height="30" Width="120"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
IsEditable="True" IsReadOnly="True"
EnumSource="enums:YesNoInherit" Margin="52,0">
<samples:MaterialEnumComboBox.Style>
<Style TargetType="samples:MaterialEnumComboBox">
<Setter Property="Text"
Value="{Binding Path=selectedModel.GenerateStatements,
UpdateSourceTrigger=PropertyChanged}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding selectedModel.GenerateStatements}">
<DataTrigger.Value>
<enums:YesNoInherit>Inherit</enums:YesNoInherit>
</DataTrigger.Value>
<Setter Property="Text">
<Setter.Value>
<MultiBinding Converter="{StaticResource inheritanceEnumValueConverter}">
<Binding Path="selectedModel.GenerateStatements"></Binding>
<Binding Path="selectedModel.DefaultGenerateStatements"></Binding>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</samples:MaterialEnumComboBox.Style>
</samples:MaterialEnumComboBox>
</Grid>
</UserControl>
这是自定义的ComboBox。没有重要的XAML。
public partial class MaterialEnumComboBox
{
private Type _enumSource;
public Type EnumSource
{
get { return _enumSource; }
set
{
_enumSource = value;
this.SetBinding(ItemsSourceProperty, new Binding { Source = Enum.GetValues(_enumSource) });
}
}
public MaterialEnumComboBox()
{
InitializeComponent();
}
}
这是MultiBinding的转换器。
public class InheritanceEnumValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.All(x => x.GetType().BaseType != typeof(Enum)))
throw new NotSupportedException();
else
{
Enum typedValue = values[0] as Enum;
Enum typedValue2 = values[1] as Enum;
string strAbbr = typedValue.Description(); // based on the Description attribute for the enum.
if (typedValue != null)
{
if (strAbbr == "Inherited")
strAbbr = typedValue2.ToString() + " " + "(" + "Inherited" + ")";
return strAbbr;
}
else
return string.Empty;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
Type enumTargetType = targetTypes.First();
var enumValue = Enum.Parse(enumTargetType, value.ToString());
return new object[] { enumValue };
}
}
这是模型。 ObservableObject来自Galasoft.MvvmLight。
public class MyModel: ObservableObject
{
private YesNoInherit _generateStatements;
private YesNoInherit _defaultGenerateStatements;
public YesNoInherit GenerateStatements
{
get { return _generateStatements; }
set
{
if (value != _generateStatements)
{
_generateStatements = value;
RaisePropertyChanged("GenerateStatements");
}
}
}
public YesNoInherit DefaultGenerateStatements
{
get { return _defaultGenerateStatements; }
set
{
if (value != _defaultGenerateStatements)
{
_defaultGenerateStatements = value;
RaisePropertyChanged("DefaultGenerateStatements");
}
}
}
}
最后是枚举。
public enum YesNoInherit
{
[Description("No")]
No = 0, ///<
[Description("Yes")]
Yes = 1, ///<
[Description("Inherited")]
Inherit = 2
}
&#34; DefaultGenerateStatements&#34;财产总是&#34;是&#34;对于这个例子。 首次出现组合框时,值为&#34;是&#34;。 我按此顺序更改组合框的值。
对于步骤4和5,DataTrigger会在我认为不应该触发时触发。我的代码有问题吗?