2012-03-15 31 views
0

3个不同的风格我有一个Gridview.Column其中内容的风格依赖于另一列由IMultiValueConverter内容改变。这部分按预期工作。如何使用基于两个不相关的触发器

如果这两个值的值相同,则此列的文字是浅灰色,否则黑。

<GridViewColumn Header="New Name" Width="300"> 
    <GridViewColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding newFileName}"> 
       <TextBlock.Style> 
        <Style> 
         <Setter Property="TextBlock.Foreground" Value="Black"></Setter> 
         <Style.Triggers> 
          <DataTrigger Value="True"> 
           <DataTrigger.Binding> 
            <MultiBinding Converter="{StaticResource FileNameColorConverter}"> 
             <!--<Binding Path="Selected"/>--> 
             <Binding Path="newFileName"/> 
             <Binding Path="fileName"/> 
            </MultiBinding> 
           </DataTrigger.Binding> 
           <Setter Property="TextBlock.Foreground" Value="LightGray"></Setter> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 
     </DataTemplate> 
    </GridViewColumn.CellTemplate> 
</GridViewColumn> 

现在我在此对象中有另一个属性绑定到此GridView。如果这是TRUE,则此列的文本应该变为红色,而不是黑色,如果它为假,则FileNameColorConverter应该决定列的样式,与现在相同。

我怎样才能做到这一点?我目前在迷失方向,不知道该把逻辑放在哪里,我相当确信在XAML中也有这种方法。

编辑

我尝试的第一个后加入另一个触发,但它并没有为我工作,如果这是要走的路,什么是错我的代码?

我添加了这个后,我的第一个</DataTrigger>但它并没有产生影响。

<DataTrigger Value="True"> 
    <DataTrigger.Binding> 
      <Binding Path="FileNameError"/> 
    </DataTrigger.Binding> 
    <Setter Property="TextBlock.Foreground" Value="Red"></Setter> 
</DataTrigger> 

回答

1

多个数据与同一数据工作独占条件触发我的完美......你能不能检查,如果你FileNameErrorTrue为atleast 1项?如果你动态改变它,那么你是否在提升PropertyChanged事件?

你可以试试我的代码,看看...

XAML:

<Page.Resources> 
    <local:MultiBindingConverter x:Key="MultiBindingConverter"/> 
    <coll:ArrayList x:Key="MyData"> 
     <local:Student Id="1" local:Student.Name="Test1" Active="False"/> 
     <local:Student Id="2" local:Student.Name="Test2" Active="True"/> 
     <local:Student Id="3" local:Student.Name="Test3" Active="False"/> 
     <local:Student Id="4" local:Student.Name="Test4" Active="False"/> 
    </coll:ArrayList> 
</Page.Resources> 
<Grid> 
    <ListBox ItemsSource="{StaticResource MyData}" 
      DisplayMemberPath="Name"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Style.Resources> 
        <SolidColorBrush 
         x:Key="{x:Static SystemColors.HighlightBrushKey}" 
         Color="Yellow"/> 
       </Style.Resources> 
       <Style.Triggers> 
        <DataTrigger Value="True"> 
         <DataTrigger.Binding> 
          <MultiBinding 
           Converter="{StaticResource 
            MultiBindingConverter}"> 
           <Binding Path="Id"/> 
           <Binding Path="Active"/> 
          </MultiBinding> 
         </DataTrigger.Binding> 
         <Setter Property="Foreground" Value="Red"/> 
        </DataTrigger> 
        <DataTrigger Value="3"> 
         <DataTrigger.Binding> 
          <Binding Path="Id"/> 
         </DataTrigger.Binding> 
         <Setter Property="Foreground" Value="Blue"/> 
        </DataTrigger> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter Property="Foreground" Value="Gray"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
</Grid> 

代码背后:

public class MultiBindingConverter : IMultiValueConverter 
{ 
    #region IMultiValueConverter Members 

    public object Convert(object[] values, Type targetType, 
    object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (values[0] != DependencyProperty.UnsetValue 
      && values[0] != null && values[1] != null) 
     { 
      var value1 = (int)values[0]; 
      var value2 = (bool)values[1]; 

      var result = (value1 > 1 && value2); 

      if (result) 
      { 
       return true; 
      } 

      return false; 
     } 

     return false; 
    } 

    public object[] ConvertBack 
     (object value, Type[] targetTypes, 
      object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

public class Student : INotifyPropertyChanged 
{ 
    public int Id 
    { 
     get; 
     set; 
    } 

    public string Name 
    { 
     get; 
     set; 
    } 


    public bool Active 
    { 
     get; 
     set; 
    } 

    public Student() 
    { } 

    public Student(int id, string name) 
    { 
     this.Id = id; 
     this.Name = name; 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 
} 
+0

哇,你可以看到我的错误...我忘了提高该属性的'NotifyPropertyChanged'。谢谢你的提示。 – stema 2012-03-15 12:23:48

1

现有的一个触发与Value="True"的附加属性后添加另一个触发。如果属性为true,则应该覆盖现有的触发器,从而允许您将该列设置为红色。但是,如果该属性为false,则触发器将不执行任何操作,并且现有触发器的效果将可见。

+0

那是我提出的想法,但它不不为我工作。我用这段代码更新了我的问题,也许你可以告诉我我做错了什么。 – stema 2012-03-15 09:35:12

+0

我忘了提高该属性的'NotifyPropertyChanged'。你向我展示的+1,我正走在正确的道路上。 – stema 2012-03-15 12:25:30

相关问题