tags:

views:

42

answers:

3

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 "*"?

+1  A: 

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.

SLaks
If I want to refernece a property in XAML won't I need to use DependencyProperties?
jiewmeng
No, you don't. You only need to make a DependencyProperty if you want to bind that property to a value (not if you want to bind a different property to its value)
SLaks
DependencyObject implements INotifyPropertyChanged, and at its core, that is how a dependency property works--it sends change notifications via the interface
Muad'Dib
Hmm, how might the code look like? sorry, I am still relatively new to more advanced WPF. Thanks in advance
jiewmeng
A: 

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 Runs which are bindable in WPF 4+) Visibility/FontWeight according to it via DataTriggers.

Shimmy
A: 

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}"
Muad'Dib