2009-12-16 53 views
0

我有一個樣式模板(下面),當我的標籤綁定更新時不更新。數據本身更新,我收到沒有綁定錯誤,所以期待一切都正確綁定它只是該樣式似乎並沒有更新。我在所有正確的地方afaik notifypropertychanged事件,所以懷疑這是問題。樣式模板在數據更新時不重新評估

感謝

<Style x:Key="CompareTemplate" TargetType="TextBlock"> 
      <!--Setter Property="Foreground" Value="#FF760000" /--> 
      <Setter Property="Foreground" Value="#FFBCBCBC" /> 
     <Style.Triggers> 
      <Trigger Value="True" Property="Tag"> 
       <Setter Property="Foreground" Value="#FF007602" /> 
      </Trigger> 
      <Trigger Value="False" Property="Tag"> 
       <Setter Property="Foreground" Value="#FF760000" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

而且我用這個模板,像這樣:

<TextBlock Style="{DynamicResource CompareTemplate}" Tag="{Binding UnitComparer.CommsID, FallbackValue=True}" Text="Comms ID:" /> 

回答

1

標籤是對象類型。我認爲你的Viewmodel會爲它設置一個布爾值。 WPF能夠在字符串和對象之間轉換,但看起來不在bool和對象之間。一個解決方案是使用的IValueConverter到布爾改變爲一個字符串:

<Window x:Class="BindToTagSpike.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:BindToTagSpike" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
     <StackPanel.Resources> 
      <local:ObjectToString x:Key="ObjectToString"/> 
      <Style x:Key="CompareTemplate" TargetType="TextBlock"> 
       <Style.Triggers> 
        <Trigger Value="True" Property="Tag"> 
         <Setter Property="Foreground" Value="Red" /> 
        </Trigger> 
        <Trigger Value="False" Property="Tag"> 
         <Setter Property="Foreground" Value="YellowGreen" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </StackPanel.Resources> 
     <TextBlock Style="{StaticResource CompareTemplate}" 
        Name="TaggedTextBlock" 
        Tag="{Binding TagValue, 
        Converter={StaticResource ObjectToString}}"/> 
     <Button Click="Button_Click">Change Style</Button>   
    </StackPanel> 
</Window> 


using System; 
using System.Windows; 
using System.Windows.Data; 
using System.ComponentModel; 

namespace BindToTagSpike 
{ 

    public partial class Window1 : Window, INotifyPropertyChanged 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
      tagValue = false; 
      TaggedTextBlock.Text = "Test"; 
      DataContext = this; 

     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      TagValue=!TagValue; 
     } 

     private bool tagValue; 
     public bool TagValue 
     { 
      get { return tagValue; } 
      set 
      { 
       tagValue = value; 
       if (PropertyChanged != null) 
        PropertyChanged(this,new PropertyChangedEventArgs("TagValue")); 
      } 

     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 

     #endregion 
    } 

    public class ObjectToString : IValueConverter 
    { 

     #region IValueConverter Members 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return value.ToString(); 
     } 

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

     #endregion 
    } 
} 
+0

靶心!謝謝! – Chris 2009-12-17 10:56:35

1

有時當預期並未被應用於您的模板/風格,WPF可能沒有想到的TargetType可能控制類型相匹配。嘗試下面的代碼,看看有沒有什麼幫助所有:

<Style x:Key="CompareTemplate" > 
        <!--Setter Property="Control.Foreground" Value="#FF760000" /--> 
        <Setter Property="Control.Foreground" Value="#FFBCBCBC" /> 
    <Style.Triggers> 
     <Trigger Value="True" Property="Control.Tag"> 
      <Setter Property="Control.Foreground" Value="#FF007602" /> 
     </Trigger> 
     <Trigger Value="False" Property="Control.Tag"> 
      <Setter Property="Control.Foreground" Value="#FF760000" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

乾杯,
Berryl

+0

好主意,但不幸的是它沒有工作 - 謝謝 – Chris 2009-12-17 10:55:05