C# WPF Textblock文本不会在组合框选定项上动态更改

C# WPF Textblock文本不会在组合框选定项上动态更改,c#,wpf,mvvm,.net-3.5,textblock,C#,Wpf,Mvvm,.net 3.5,Textblock,我有一个MVVM应用程序。在主WPF窗口中,我有一个combox和一个textblock以及其他控件 当我从combobox中选择一个值时,textblock文本应该根据combobox中所选的项目(取决于combobox中所选项目的id)动态更改其文本值 我的问题是,当我在组合框中选择一个项目时,textblock文本不会更改,它始终具有默认值。有什么办法解决这个问题吗 我只想使用xaml来实现这一点 型号: public class Item { #region Constructo

我有一个MVVM应用程序。在主WPF窗口中,我有一个combox和一个textblock以及其他控件

当我从combobox中选择一个值时,textblock文本应该根据combobox中所选的项目(取决于combobox中所选项目的id)动态更改其文本值

我的问题是,当我在组合框中选择一个项目时,textblock文本不会更改,它始终具有默认值。有什么办法解决这个问题吗

我只想使用xaml来实现这一点

型号

public class Item
{
    #region Constructors

    public Item() { }

    public Item(int id, string desc)
    {
        this.Id = id;
        this.Desc = desc;
    }

    #endregion

    #region Properties

    public int Id
    {
        get;
        set;
    }

    public string Desc
    {
        get;
        set;
    }

    #endregion

    public override string ToString()
    {
        return this.Desc;
    }
}
private ObservableCollection<Item> _myItems;
public ObservableCollection<Item> MyItems
{
    get { return _myItems; }
    set { _myItems= value; }
}
<ComboBox x:Name="MyWPFCombo"           
          ItemsSource="{Binding MyItems}"/>

<TextBlock Padding="5 10 0 0">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
           <Setter Property="Text" Value="Select the items:" />
           <Style.Triggers>
               <DataTrigger Binding="{Binding ElementName=MyWPFCombo, Path=Id}" Value="10">
                   <Setter Property="Text" Value="Select the old items:" />
               </DataTrigger>                               
           </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
视图模型中的MVVM属性

public class Item
{
    #region Constructors

    public Item() { }

    public Item(int id, string desc)
    {
        this.Id = id;
        this.Desc = desc;
    }

    #endregion

    #region Properties

    public int Id
    {
        get;
        set;
    }

    public string Desc
    {
        get;
        set;
    }

    #endregion

    public override string ToString()
    {
        return this.Desc;
    }
}
private ObservableCollection<Item> _myItems;
public ObservableCollection<Item> MyItems
{
    get { return _myItems; }
    set { _myItems= value; }
}
<ComboBox x:Name="MyWPFCombo"           
          ItemsSource="{Binding MyItems}"/>

<TextBlock Padding="5 10 0 0">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
           <Setter Property="Text" Value="Select the items:" />
           <Style.Triggers>
               <DataTrigger Binding="{Binding ElementName=MyWPFCombo, Path=Id}" Value="10">
                   <Setter Property="Text" Value="Select the old items:" />
               </DataTrigger>                               
           </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
private observedcollection\u myItems;
公共可观测集合MyItems
{
获取{return\u myItems;}
设置{u myItems=value;}
}
查看

public class Item
{
    #region Constructors

    public Item() { }

    public Item(int id, string desc)
    {
        this.Id = id;
        this.Desc = desc;
    }

    #endregion

    #region Properties

    public int Id
    {
        get;
        set;
    }

    public string Desc
    {
        get;
        set;
    }

    #endregion

    public override string ToString()
    {
        return this.Desc;
    }
}
private ObservableCollection<Item> _myItems;
public ObservableCollection<Item> MyItems
{
    get { return _myItems; }
    set { _myItems= value; }
}
<ComboBox x:Name="MyWPFCombo"           
          ItemsSource="{Binding MyItems}"/>

<TextBlock Padding="5 10 0 0">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
           <Setter Property="Text" Value="Select the items:" />
           <Style.Triggers>
               <DataTrigger Binding="{Binding ElementName=MyWPFCombo, Path=Id}" Value="10">
                   <Setter Property="Text" Value="Select the old items:" />
               </DataTrigger>                               
           </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

您需要提供两件事

1) 您需要设置ComboBox的
SelectedValuePath

 <ComboBox x:Name="MyWPFCombo"       SelectedValuePath="Id"  
      ItemsSource="{Binding MyItems}" />

你需要提供两件事

1) 您需要设置ComboBox的
SelectedValuePath

 <ComboBox x:Name="MyWPFCombo"       SelectedValuePath="Id"  
      ItemsSource="{Binding MyItems}" />

您正在绑定到组合框的
Id
属性,但是该属性不存在。您需要使用
SelectedItem
属性来访问所选项目及其属性:

<DataTrigger Binding="{Binding SelectedItem.Id, ElementName=MyWPFCombo}" Value="10">
    <Setter Property="Text" Value="Select the old items:" />
</DataTrigger>   

您正在绑定到您的
组合框的
Id
属性,但是该属性不存在。您需要使用
SelectedItem
属性来访问所选项目及其属性:

<DataTrigger Binding="{Binding SelectedItem.Id, ElementName=MyWPFCombo}" Value="10">
    <Setter Property="Text" Value="Select the old items:" />
</DataTrigger>   


类是否也可以INotifyPropertyChanged?@Siegfried.V无需,绑定在控件上(元素绑定),正如您所看到的,我们正在使用ComboBox的属性
SelectedValue
。因此不需要
INotifyPropertyChanged
ok,但如果我们更改combobox的ObservableCollection(添加/删除项),它将自动更新?对不起,我也在使用这种绑定,我也会问,因为我没有完全掌握it@Siegfried.V好奇很好。:)如果集合是List类型,我会同意的。但MyItems属于ObservableCollection类型,它是一种通用的动态数据收集,使用接口“INotifyCollectionChanged”。因此,即使再次添加,集合也会更新。@user1624552这是否解决了您的问题?或者您仍然面临这个问题?类是否也可能被INotifyPropertyChanged?@Siegfried.V不需要,绑定在控件(元素绑定)上,正如您看到的,我们正在使用ComboBox的属性
SelectedValue
。因此不需要
INotifyPropertyChanged
ok,但如果我们更改combobox的ObservableCollection(添加/删除项),它将自动更新?对不起,我也在使用这种绑定,我也会问,因为我没有完全掌握it@Siegfried.V好奇很好。:)如果集合是List类型,我会同意的。但MyItems属于ObservableCollection类型,它是一种通用的动态数据收集,使用接口“INotifyCollectionChanged”。因此,即使再次添加,集合也会更新。@user1624552这是否解决了您的问题?还是你仍然面临这个问题?