依赖属性未更新?
我不知道我是否正确使用了依赖属性,但我的视图似乎永远不会更新。我有一个样式触发器来更改元素的样式。看起来代码运行了,但视图没有更新。这是我第一次使用依赖属性和依赖属性。我可能使用错误
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>
I don't know if I am using dependency properties right, but it seems my View never updates. I have a style trigger to change the styles of my elements. It seems the code runs, but the view is not updated. This is my 1st time using a Dependency Property & I maybe using it wrong
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>
The TextBlock is always red nv bold
UPDATE: Prefixing the tab header with *
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TextBlock 没有属性“vm:EditorTabViewModel.CanSave”,因此样式触发器将被忽略。
使用 DataTriggers 代替:
选项卡项模板:
TextBlock doesn't have property "vm:EditorTabViewModel.CanSave", so style triggers are ignored.
Use DataTriggers instead:
Tab item template:
我可以在您的代码中指出两件事 -
1 您的样式缺少 TargetType -
TargetType="TextBlock"
另外,您在触发器中使用的 Property 在我看来是错误的,您应该使用 < a href="http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx" rel="nofollow">DataTrigger 像这样
2 您的内容属性只需要这个
I can point out two things in your code -
1 Your style is missing the TargetType -
TargetType="TextBlock"
also, you are using Property in your trigger which looks wrong to me, you should be using DataTrigger like this
2 Your Content property needs only this