我有一个comboBox,它有一个数据触发器,它根据VM中的.NET属性值设置其SelectedIndex。我的问题是我无法让setter设置Selected Index。无法使用DataTrigger在Combobox中设置SelectedValue
ItemSource基于枚举数组。 窗口的DataContext是具有调制和带宽属性的VM。
我是新来的WPF,所以我确定我不理解绑定正确,但我拉我的头发!感谢您的帮助提前。
这是样式。
<Style x:Key="BWCombBoxStyle" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
<DataTrigger
Binding="{Binding Modulation}" Value="P25">
<Setter Property="SelectedIndex" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
这里的组合框:
<ComboBox Name="bandwidth"
Height="Auto" Width="70"
Style="{StaticResource BWCombBoxStyle}"
ItemsSource="{Binding BandwidthOptions, Mode=OneWay, ValidatesOnDataErrors=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding IFBandwidth, Mode=TwoWay, ValidatesOnDataErrors=True,
NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"/>
以下是.NET属性在我的VM:
public TMod Modulation
{
get { return modulation_; }
set { modulation_ = value; NotifyPropertyChanged("Modulation"); }
}
public Channel.TBnd IFBandwidth
{
get { return chan_.IFBandwidth; }
set
{
chan_.IFBandwidth = value;
NotifyPropertyChanged("IFBandwidth");
}
}
public Channel.TBnd[] BandwidthOptions
{
get
{
return (Channel.TBnd[])System.Enum.GetValues(typeof(Channel.TBnd));
}
}
这里是枚举:
public enum TMod
{
FM = 0,
AM = 1,
P25 = 2,
TRK = 3
}
public enum TBnd
{
Std = 0,
Nar = 1,
Wide = 2,
XWide = 3
}
如果你不明白绑定读** [this](http://msdn.microsoft.com/zh-cn/ -us /库/ ms752347.aspx)**。 – 2011-06-07 21:10:30
您是否在VisualStudio的输出窗口中看到绑定错误? – 2011-06-07 21:26:24
作为另外设置SelecteValuePath或使用SelectedItem绑定来代替?需要触发器吗?你应该能够在视图模型中设置IFBandwith,并且它将在组合框中设置选定的值。 – 2011-06-07 21:39:51