WPF - 为什么 datatrigger 不触发有什么想法?

发布于 2024-09-07 15:47:02 字数 3050 浏览 6 评论 0原文

我的列表框定义如下。 “属性”是一个 BindingList,我正在更改其中一项,但图像样式没有更新。有什么想法我可能会错过吗?

            <ListBox x:Name="lstProperties" 
                     Margin="0,0,5,0" 
                     ItemsSource="{Binding Properties}" 
                     SelectedItem="{Binding CurrentProperty}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="16"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Image>
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Setter Property="Source" Value="Images/HouseRed_16.png"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding SuitableApplicationCount, Converter={StaticResource greaterThanConverter}, ConverterParameter=0}" Value="True">
                                                <Setter Property="Source" Value="Images/HouseYellow_16.png"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding InterestedApplicationCount, Converter={StaticResource greaterThanConverter}, ConverterParameter=0}" Value="True">
                                                <Setter Property="Source" Value="Images/HouseAmber_16.png"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding MatchedApplicationId, Converter={StaticResource isNullOrEmptyConverter}}" Value="False">
                                                <Setter Property="Source" Value="Images/HouseGreen_16.png"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                            <TextBlock Grid.Column="1" VerticalAlignment="Center">
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}Id: {0}, Plot: {1}">
                                        <Binding Path="Id" FallbackValue="" />
                                        <Binding Path="Plot" FallbackValue=""/>
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

My listbox is defined below. "Properties" is a BindingList which I am changing one of the items but the Image style is not being updated. Any ideas what I might be missing?

            <ListBox x:Name="lstProperties" 
                     Margin="0,0,5,0" 
                     ItemsSource="{Binding Properties}" 
                     SelectedItem="{Binding CurrentProperty}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="16"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Image>
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Setter Property="Source" Value="Images/HouseRed_16.png"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding SuitableApplicationCount, Converter={StaticResource greaterThanConverter}, ConverterParameter=0}" Value="True">
                                                <Setter Property="Source" Value="Images/HouseYellow_16.png"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding InterestedApplicationCount, Converter={StaticResource greaterThanConverter}, ConverterParameter=0}" Value="True">
                                                <Setter Property="Source" Value="Images/HouseAmber_16.png"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding MatchedApplicationId, Converter={StaticResource isNullOrEmptyConverter}}" Value="False">
                                                <Setter Property="Source" Value="Images/HouseGreen_16.png"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                            <TextBlock Grid.Column="1" VerticalAlignment="Center">
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}Id: {0}, Plot: {1}">
                                        <Binding Path="Id" FallbackValue="" />
                                        <Binding Path="Plot" FallbackValue=""/>
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2024-09-14 15:47:02

让您的模型实现 INotifyPropertyChanged。
我包含了一些示例代码:

public class MyModel : ViewModelBase
{
  private int _suitableApplicationCount;
  public int SuitableApplicationCount
  {
     get { return _suitableApplicationCount; }
     set
     {
        _suitableApplicationCount = value;
        OnPropertyChanged("SuitableApplicationCount");
     }
  }

  public int _interestedApplicationCount;
  public int InterestedApplicationCount
  {
     get { return _interestedApplicationCount; }
     set
     {
        _interestedApplicationCount = value;
        OnPropertyChanged("InterestedApplicationCount");
     }
  }

  public int? _matchedApplicationId;
  public int? MatchedApplicationId
  {
     get { return _matchedApplicationId; }
     set
     {
        _matchedApplicationId = value;
        OnPropertyChanged("MatchedApplicationId");
     }
  }
}

public abstract class ViewModelBase : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string propertyName)
  {
     PropertyChangedEventHandler handler = PropertyChanged;

     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(propertyName));
     }
  }
}

Make your model implement INotifyPropertyChanged.
I included some sample code:

public class MyModel : ViewModelBase
{
  private int _suitableApplicationCount;
  public int SuitableApplicationCount
  {
     get { return _suitableApplicationCount; }
     set
     {
        _suitableApplicationCount = value;
        OnPropertyChanged("SuitableApplicationCount");
     }
  }

  public int _interestedApplicationCount;
  public int InterestedApplicationCount
  {
     get { return _interestedApplicationCount; }
     set
     {
        _interestedApplicationCount = value;
        OnPropertyChanged("InterestedApplicationCount");
     }
  }

  public int? _matchedApplicationId;
  public int? MatchedApplicationId
  {
     get { return _matchedApplicationId; }
     set
     {
        _matchedApplicationId = value;
        OnPropertyChanged("MatchedApplicationId");
     }
  }
}

public abstract class ViewModelBase : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string propertyName)
  {
     PropertyChangedEventHandler handler = PropertyChanged;

     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(propertyName));
     }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文