我有列表视图,我想根据绑定值更改其样式,所以我做了以下
<Style x:Key="ListBoxItemStyleDragAndDropNew" TargetType="{x:Type ListBoxItem}">
<Setter Property="Tag" Value="{Binding IsReadedBefore}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ControlTemplate.Triggers>
<Trigger Property="Tag" Value="true">
<Setter Property="Background" TargetName="Bd" Value="#7F084D78"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" TargetName="Bd" Value="4,0,0,0"></Setter>
<Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource btnColorNew}"/>
<Setter Property="Margin" TargetName="Bd" Value="0"/>
<Setter Property="Padding" TargetName="Bd" Value="4,0,0,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
IsRead是布尔值,我甚至试过了<Setter Property="ListBoxItem.Tag" Value="{Binding IsReadedBefore}"/>
,但只是改变IsReadedBefore= true
任何想法如何解决
答案 0 :(得分:2)
尝试下一种方法,数据触发器将更改内容模板backgrownd,此方法将保存原始列表框项控件模板。 1. Xaml代码:
<Window x:Class="ListBoxStyleBindngHelpAttempt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:listBoxStyleBindngHelpAttempt="clr-namespace:ListBoxStyleBindngHelpAttempt"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="ListBoxItemStyleDragAndDropNewOriginalBackground" Color="Wheat"/>
<Style x:Key="ListBoxItemStyleDragAndDropNew" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="{StaticResource ListBoxItemStyleDragAndDropNewOriginalBackground}"></Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type listBoxStyleBindngHelpAttempt:Model}">
<Grid x:Name="Grid" Width="200" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" Background="Transparent"/>
<Button Grid.Column="1" Content="Press To Change Status" Command="{Binding ChangeIsReadedStatusCommand}"></Button>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsReaded}" Value="True">
<Setter TargetName="Grid" Property="Background" Value="GreenYellow"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding IsReaded}" Value="False">
<Setter TargetName="Grid" Property="Background" Value="{StaticResource ListBoxItemStyleDragAndDropNewOriginalBackground}"></Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Window.DataContext>
<listBoxStyleBindngHelpAttempt:MainViewModel/>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding Titles}" ItemContainerStyle="{StaticResource ListBoxItemStyleDragAndDropNew}"></ListBox>
</Grid>
2.查看模型:
public class MainViewModel:BaseObservableObject
{
public MainViewModel()
{
Titles = new ObservableCollection<Model>(new List<Model>
{
new Model {Title = "War and Peace, Tolstoy"},
new Model {Title = "Anna Karenine, Tolstoy"},
new Model {Title = "Uncle Vanya, Chekhov"},
});
}
public ObservableCollection<Model> Titles { get; set; }
}
public class Model:BaseObservableObject
{
private ICommand _changeIsReadedStatusCommand;
private bool _isReaded;
private string _title;
public Model()
{
IsReaded = false;
}
public bool IsReaded
{
get { return _isReaded; }
set
{
_isReaded = value;
OnPropertyChanged();
}
}
public string Title
{
get { return _title; }
set
{
_title = value;
OnPropertyChanged();
}
}
public ICommand ChangeIsReadedStatusCommand
{
get
{
return _changeIsReadedStatusCommand ??
(_changeIsReadedStatusCommand = new RelayCommand(ChangeIsReadedStatus));
}
}
private void ChangeIsReadedStatus()
{
IsReaded = !IsReaded;
}
}
如果您遇到代码问题,我很乐意提供帮助。 问候。