【问题标题】:WPF - Any ideas why datatrigger not firing?WPF - 为什么datatrigger不触发的任何想法?
【发布时间】:2010-07-01 14:48:06
【问题描述】:

我的列表框定义如下。 “属性”是一个 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>

【问题讨论】:

    标签: wpf styles datatemplate


    【解决方案1】:

    让您的模型实现 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));
         }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 1970-01-01
      • 2010-09-10
      • 2015-02-07
      • 2021-11-28
      • 1970-01-01
      • 2012-03-05
      • 2010-12-02
      • 2010-12-10
      相关资源
      最近更新 更多