【问题标题】:Unable to set SelectedValue in Combobox using DataTrigger无法使用 DataTrigger 在 Combobox 中设置 SelectedValue
【发布时间】:2011-09-10 10:13:39
【问题描述】:

我有一个组合框,它有一个数据触发器,它根据 VM 中的 .NET 属性值设置其 SelectedIndex。我的问题是我无法让 setter 设置选定索引。

ItemSource 基于枚举数组。 Window 的 DataContext 是具有 Modulation 和 Bandwidth 属性的 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}"/>

这是我的 VM 中的 .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
    }

【问题讨论】:

  • 如果您不了解绑定,请阅读 this
  • VisualStudio 的输出窗口是否出现绑定错误?
  • 是不是像额外设置 SelecteValuePath 或使用 SelectedItem 绑定一样简单?需要触发器吗?您应该能够在视图模型中设置 IFBandwith ,它将在组合框中设置选定的值。
  • @Jerod:听起来更像是一个答案而不是评论。

标签: c# wpf xaml combobox datatrigger


【解决方案1】:

更改您的 ComboBox 绑定以使用 SelectedValue 而不是 SelectedPath。这将在值更改时正确设置 IFBandwidth 视图模型属性。

触发器的具体用途是什么?将 Modulation 属性更改为类似这样可能是更好的选择...

public TMod Modulation
{
    get { return modulation_; }
    set
    {
        modulation_ = value; 
        NotifyPropertyChanged("Modulation");

        if( modulation == TMod.P25 )
        {
            IFBandwith = TBand.Wide;
        }
    }
 }

【讨论】:

  • 我实际上按照您的建议更改了 VM,并且效果很好。愚蠢的问题,但我没有看到 SelectedPath 在 ComboBox XAML 中的使用位置。我想我一定不理解绑定语法。
  • 对不起,我的意思是 SelectedValuePath。虽然,再想一想,我不认为它会解决你的问题。
  • 我想我希望这一切都在 XAML 中完成。你明白为什么它不起作用吗?我想知道是否因为类型不兼容而需要转换器?有趣的是,我可以使用将 IsEnabled 属性设置为 false 的设置器禁用组合框,因此我知道触发器正在工作。
  • 这肯定是转换问题。可能需要一个字符串到整数的转换器。我不知道完整的设计要求,但据我所知,这是数据驱动的逻辑,在 VM 中是有意义的。我个人只使用触发器来操纵视图的外观(可见性、颜色等),而不是更改模型的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 2017-11-02
  • 1970-01-01
  • 2023-04-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多