2015-12-22 78 views
1

我已经列表视图,我想根据结合的值来改变它的风格,所以我做了以下标签绑定,并引发

<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任何想法改变如何解决这个问题

+1

使用。在你的代码中,你正在比较布尔与对象 –

+1

1)为什么你必须重写控制模板?你可以使用Style.Triggers。 2)使用DataTrigger或写一个转换器将对象转换为布尔值。 – user1672994

回答

2

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

,我会很乐意帮助,如果你将有代码的问题。 此致敬礼。