我的列表框定义如下。 “属性”是一个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>
答案 0 :(得分:0)
让你的模型实现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));
}
}
}