标签绑定和触发器

本文关键字:触发器 绑定 标签 | 更新日期: 2023-09-27 18:27:38

我有列表视图,我想根据绑定值更改它的样式,所以我做了以下

<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没有任何变化,也不知道如何解决

标签绑定和触发器

尝试下一种方法,数据触发器将更改内容模板background,这种方法将保存原始的列表框项控制模板。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;
    }
}

如果您在代码方面有问题,我很乐意为您提供帮助。当做