我有一个comboBox,它有一个数据触发器,它根据VM中的.NET属性值设置它的SelectedIndex。我的问题是我不能让setter来设置选定的索引。
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:
<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属性:
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
}发布于 2011-06-08 05:53:57
将您的ComboBox绑定更改为使用SelectedValue而不是SelectedPath。这将在值更改时正确设置IFBandwidth视图模型属性。
触发器到底是用来做什么的?将您的Modulation属性更改为如下所示可能是更好的选择。
public TMod Modulation
{
get { return modulation_; }
set
{
modulation_ = value;
NotifyPropertyChanged("Modulation");
if( modulation == TMod.P25 )
{
IFBandwith = TBand.Wide;
}
}
}https://stackoverflow.com/questions/6271610
复制相似问题