简体   繁体   中英

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

I have tabs representing documents, something like in Word. My TabControl is bound to an ObservableCollection<TabViewModel> . TabViewModel has a property CanSave indicating whether a document can be save. 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 . And add a trigger. But what about prefix the "*"?

You don't need to make a DependencyProperty ; you just need to implement INotifyPropertyChanged .

You can bind the property to the Visibility of a separate <TextBlock>*</TextBlock> and to the weight of the title using triggers.

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.

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.

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}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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