0

I have two TextBlock whose TextProperty is binded to two properties in class. Namely, StackingText and StackingRate... I want that if StackingRate is null or empty, the StackingText should be set to null or empty since the default value for StackingText is "Is charged at".

<TextBlock Name="stackingText"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingText}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>
<TextBlock Name="stackingRate"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingRate}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>

Actually i am printing this page, so it doesn't make sense that when stackingRate is empty, stackingText has value.

Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104

3 Answers3

2

You could use a DataTrigger for the stackingText TextBlock where you bind to Text for the stackingRate `TextBlock' and if the Value is "" (null or empty) then you set the Text to "" as well. This will also leave the property StackingText untouched because of the way that Triggers work

<TextBlock Name="stackingText"
           Margin="270,215,0,0"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding Path=StackingText}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=stackingRate, Path=Text}"
                             Value="">
                    <Setter Property="Text" Value=""/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<TextBlock Name="stackingRate"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingRate}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • Thanks Meleak....I wonder how much knowledge u guys have :-) Maleak can you provide me some references which can help me learn WPF basics, advance and everything.....i just want to learn everything about this technology.... – Pankaj Upadhyay Feb 06 '11 at 18:26
  • 1
    @Pankaj Upadhyay: No problem :) For WPF Books, have a look at this SO question: http://stackoverflow.com/questions/9591/what-wpf-books-would-you-recommend – Fredrik Hedblad Feb 06 '11 at 18:34
1

You can accomplish this in a trigger, as Meleak points out. If you were using MVVM, though, you wouldn't even have needed to ask this question.

The more I work with WPF, the clearer it is to me that everything's simpler if you just implement a view model. Yes, it's a little tedious to always have to write the likes of

public string StackingText
{
   get { return _Model.StackingText; }
}

over and over just to support a view. (It'd be nice if C# had a little syntax construct that implemented view model getters and setters automatically.) But once you have that, it's obvious how to modify it to inject logic:

public string StackingText
{
   get 
   { 
      return _Model.StackingRate == 0
         ? null
         : _Model.StackingText; 
   }
}
Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • Well i had to develop a register maintenance system for my family cotton merchant business so i started coding....Learned that WPF is the most advanced programming technology so made use of this.....Therefor i don't know much about MVVM...You can provide me links to WPF/MVVM reference which help me study this in detail – Pankaj Upadhyay Feb 06 '11 at 18:32
1

An easy reusable solution would be to write a converter which takes your default string as a parameter. This converter can decide to only show the text, when your rate is set.

UPDATE:

For an example how to do this, look at this question: Conditional element in xaml depending on the binding content

Community
  • 1
  • 1
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161