2015-12-22 79 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; 
    } 
} 

,我會很樂意幫助,如果你將有代碼的問題。 此致敬禮。