依赖属性不更新?

时间:2010-10-19 07:10:50

标签: c# wpf mvvm observablecollection

我不知道我是否正确使用依赖属性,但似乎我的View永远不会更新。我有一个样式触发器来改变我的元素的样式。似乎代码运行,但视图未更新。这是我第一次使用依赖属性&我可能用错了

C#

public bool CanSave
{
    get { return (bool)GetValue(CanSaveProperty); }
    set { SetValue(CanSaveProperty, value); }
}

public static readonly DependencyProperty CanSaveProperty =
    DependencyProperty.Register("CanSave", typeof(bool), typeof(EditorTabViewModel), new PropertyMetadata(false));

public string Content
{
    get { return _content; }
    set
    {
        if ((bool)GetValue(CanSaveProperty) == false)
        {
            SetValue(CanSaveProperty, true);
            RaisePropertyChanged("CanSave");
        } 
        _content = value;
    }
}

XAML

<TabControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <Style x:Key="CanSaveIndicatorHeader">
                    <Style.Triggers>
                        <Trigger Property="vm:EditorTabViewModel.CanSave" Value="true">
                            <Setter Property="TextBlock.FontWeight" Value="Bold" />
                        </Trigger>
                        <Trigger Property="vm:EditorTabViewModel.CanSave" Value="false">
                            <Setter Property="TextBlock.Foreground" Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Text="{Binding TabTitle}" Padding="0,0,10,0" Style="{StaticResource CanSaveIndicatorHeader}" />
            <Button Content="X" Command="{Binding CloseCommand}" FontSize="10" FontWeight="Bold" Padding="3,0">
            </Button>
        </StackPanel>
    </DataTemplate>
</TabControl.ItemTemplate>

TextBlock始终为红色nv粗体

更新:使用*

前缀标签页眉
<StackPanel Orientation="Horizontal">
    <TextBlock Text="* " x:Name="TabTitleSaveIndicator" Visibility="Collapsed" />
    <TextBlock Text="{Binding TabTitle}" x:Name="TabTitle" Padding="0,0,10,0" />
    <Button Content="X" Command="{Binding CloseCommand}" FontSize="10" FontWeight="Bold" Padding="3,0">
    </Button>
</StackPanel>
<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding CanSave}" Value="True">
        <Setter TargetName="TabTitle" Property="FontWeight" Value="Bold" />
        <Setter TargetName="TabTitleSaveIndicator" Property="Visibility" Value="Visible" />
    </DataTrigger>
</DataTemplate.Triggers>

2 个答案:

答案 0 :(得分:3)

TextBlock没有属性“vm:EditorTabViewModel.CanSave”,因此会忽略样式触发器。 改为使用DataTriggers:

public class TabItemModel : INotifyPropertyChanged
{
    public string TabTitle { get; set; }

    private bool canSave;

    public bool CanSave
    {
        get { return canSave; }
        set
        {
            canSave = value;
            OnPropertyChanged("CanSave");
        }
    }
//...
}

标签项模板:

<TabControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock x:Name="text" Text="{Binding TabTitle}" Padding="0,0,10,0"/>
                <Button Content="X" Command="{Binding CloseCommand}" FontSize="10" FontWeight="Bold" Padding="3,0">
                </Button>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding CanSave}" Value="true">
                    <Setter TargetName="text" Property="TextBlock.FontWeight" Value="Bold" />
                </DataTrigger>
                <DataTrigger Binding="{Binding CanSave}" Value="false">
                    <Setter TargetName="text" Property="TextBlock.Foreground" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </TabControl.ItemTemplate>

答案 1 :(得分:0)

我可以在你的代码中指出两件事 -

1你的风格缺少TargetType -

TargetType =“TextBlock”

另外,你在你的触发器中使用了我看起来不对的属性,你应该像这样使用DataTrigger

<DataTrigger Binding="{Binding Path=CanSave}" Value="true"> 
  • 如果CanSave和TabTitle属性都在同一个VM中。

2您的内容属性仅需

 public string Content  
    {  
         get { return _content; }  
         set  
         {  
              if (CanSave == false)  
              {  
                 CanSave=true;  
              }   
              _content = value;  
         }  
     }