简体   繁体   English

标签可以保存时如何加粗并在文本块上添加“ *”

[英]How to bold & add “*” to TextBlock when Tab Can be saved

I have tabs representing documents, something like in Word. 我有代表文档的标签,就像在Word中一样。 My TabControl is bound to an ObservableCollection<TabViewModel> . 我的TabControl绑定到ObservableCollection<TabViewModel> TabViewModel has a property CanSave indicating whether a document can be save. TabViewModel有一个属性CanSave指示文件是否可以保存。 When it can be saved, I want to bold it and prefix it with an "*". 当可以保存它时,我想将其加粗并以“ *”作为前缀。 How can I do this? 我怎样才能做到这一点? I think I need to 1st make CanSave a DependencyProperty . 我认为我需要1使CanSave一个DependencyProperty And add a trigger. 并添加一个触发器。 But what about prefix the "*"? 但是前缀“ *”呢?

You don't need to make a DependencyProperty ; 您不需要创建DependencyProperty you just need to implement INotifyPropertyChanged . 您只需要实现INotifyPropertyChanged

You can bind the property to the Visibility of a separate <TextBlock>*</TextBlock> and to the weight of the title using triggers. 您可以使用触发器将属性绑定到单独的<TextBlock>*</TextBlock>的“ Visibility ”,以及标题的权重。

A simple (maybe ugly, but should definitely work): 一个简单的(可能很难看,但绝对可以工作):

void CanSave(....)
{
   bool canSave = GetValueBlahBlah();
   if (tb.IsVisible != canSave)
       tb.Visibility = canSave ? Visibility.Visible : Visibility.Collapsed;
}

tb represents the TextBlock you wanna show and hide according to the CanSave state. tb表示您要根据CanSave状态显示和隐藏的TextBlock。

You might also wanna create a DependencyProperty as you said and set the TextBlock s (you will have to use a separate TextBlock for the star - or use Run s which are bindable in WPF 4+) Visibility / FontWeight according to it via DataTrigger s. 你也可能想创建一个DependencyProperty如你所说,并设置TextBlock S(你将不得不使用的星级单独的TextBlock -或使用Run ,这种是在WPF绑定4+) Visibility / FontWeight根据通过它DataTrigger秒。

You could also set the titles of your tabs via binding.... 您还可以通过绑定设置选项卡的标题。

<TabControl >
   <TabItem >
          <TabItem.Header>
                 <TextBlock Text="{Binding TabTitle1}" />
          <TabItem.Header>
</TabControl>

and then set the title on your data model 然后在数据模型上设置标题

Tab1Title="* " + "some nice tab title";

you could also use binding to set the font to bold.... 您还可以使用绑定将字体设置为粗体。

FontWeight="{Binding Tab1FontWeight}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM