WPF: How to make empty TextBlock not to occupy space? - wpf

Let's say that I have a simple layout such as this:
<StackPanel>
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text="{Binding Path=ShortDescription}" />
<TextBlock Text="{Binding Path=LongDescription}" />
</StackPanel>
Now when I have ShortDescription set to null or empty string there's still a gap in place of second TextBlock. Is there some property to prevent an empty textblock from occupying space? Or should I use some other control?
Thanks.

You want to set the visibility of the textbox to "Collapsed".
Visibility can be either:
Visible - Self explanatory
Hidden - Invisible but still takes up space
Collapsed - Invisible and takes up no space
Edit: You should probably set up a trigger, like so:
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>

You may want to try this:
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
This should fix the empty space issue based on a Null / Empty Binding.

Related

WPF override property specified on element from style trigger

I have two TextBlock Run elements with different colors (one is explicitly set on the element). I want them both to change color to red when the value Foo is zero, using the same style. Is this possible somehow? I would rather not duplicate the Style. This is what I want to work:
<Style x:Key="ForegroundStyleTrigger" TargetType="Run">
<Style.Triggers>
<DataTrigger Binding="{Binding Foo}" Value="0">
<Setter Property="Foreground" Value="{StaticResource RedBrush}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBlock>
<Run Text="{Binding Foo, Mode=OneWay}"
Style="{StaticResource ForegroundStyleTrigger}"/>
<Run Text="{Binding Bar, Mode=OneWay}"
Foreground="Blue"
Style="{StaticResource ForegroundStyleTrigger}"/>
</TextBlock>
But since the locally defined color (the one defined on the element) takes precedence over style triggers, nothing happens and the text stays blue for that text run.
Question: Can I override a TextBlocks runs color from a style resource?
If not, how can achieve the expected result without duplicating the style resource?
Can I override a TextBlocks runs color from a style resource?
No, you cannot override a local value using a style setter.
If not, how can achieve the expected result without duplicating the style resource?
If you want the Foreground of the second Run element to be Blue by default and Red only if the Foo source property returns "0" you could create another style and base this one on your existing one:
<Style x:Key="ForegroundStyleTrigger" TargetType="Run">
<Style.Triggers>
<DataTrigger Binding="{Binding Foo}" Value="0">
<Setter Property="Foreground" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="BlueByDefault" BasedOn="{StaticResource ForegroundStyleTrigger}" TargetType="Run">
<Setter Property="Foreground" Value="Blue" />
</Style>
<TextBlock>
<Run Text="{Binding Foo, Mode=OneWay}" Style="{StaticResource ForegroundStyleTrigger}"/>
<Run Text="{Binding Bar, Mode=OneWay}" Style="{StaticResource BlueByDefault}"/>
</TextBlock>
But you cannot set the Foreground property to a local value if you want your style setters to apply.
You would use Style inheritance to achieve what you're asking. The trick then is that all properties must be in the Style as local explicitly set values override implicitly set Style values:
<Style x:Key="ForegroundStyleTrigger" TargetType="Run">
<Style.Triggers>
<DataTrigger Binding="{Binding Foo}" Value="0">
<Setter Property="Foreground" Value="{StaticResource RedBrush}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="BlueForegroundStyleTrigger" BasedOn="{StaticResource ForegroundStyleTrigger}" TargetType="Run">
<Setter Property="Foreground" Value="Blue"/>
</Style>
<TextBlock>
<Run Text="{Binding Foo, Mode=OneWay}"
Style="{StaticResource ForegroundStyleTrigger}"/>
<Run Text="{Binding Bar, Mode=OneWay}"
Style="{StaticResource BlueForegroundStyleTrigger}"/>
</TextBlock>

not to set Hyperlink when NavigateUri is null

I am using Hyperlink in TextBlock. The problem I am facing is when NavigateUri is null, I don't want to set Hyperlink or use default style, so that there is no difference between TextBlock and Hyperlink. How to do this?
The code that I am using is this:
<TextBlock TextWrapping="Wrap">
<Hyperlink NavigateUri="{Binding Path=Href}" RequestNavigate="Hyperlink_RequestNavigate">
<Run Text="{Binding Path=Body}"/>
</Hyperlink>
</TextBlock>
Sometimes Href is null. That time I don't have to set NavigateUri.
The solution I used is using DataTrigger to check Href value, if is equals to Null, set the related properties to imitate TextBlock's style
<Style TargetType="{x:Type Hyperlink}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Href}" Value="{x:Null}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="TextBlock.TextDecorations" Value="{x:Null}" />
<Setter Property="Cursor" Value="Arrow" />
</DataTrigger>
</Style.Triggers>
</Style>
Null value:
!Null Value:

WPF ComboBox that shows nothing selected when disabled (IsEnabled == false)

I'm thinking out different ways to have a WPF ComboBox show blank as if nothing is selected when IsEnabled is set to false. Like always I'm trying to do this without having to redefine the whole control template for the ComboBox which is always a struggle I have with WPF. If anybody has any solutions more elegant than redefining the whole ComboBox control template please let me know.
The reason for what I'm trying to do is I have a CheckBox that represents an "All" option and when checked it disables the ComboBox which is used to pick only a single individual item. If my CheckBox is checked it is sometimes confusing to the users to see a value remaining in the ComboBox since that value has no meaning in that state of the UI.
Another requirement is that the solution cannot modify the SelectedValue, SelectedIndex, or SelectedItem values of the ComboBox since I would like to retain the previuosly selected item in the case that the users unchecks the "All" CheckBox.
Solution based on HCL's answer:
<ComboBox IsEnabled="{Binding ElementName=myCheckBox, Path=IsChecked}"
ItemsSource="{Binding Path=MyItems}"
SelectedValue="{Binding Path=MySelectedItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<ContentControl x:Name="content" Content="{Binding MyItemDescription}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ComboBox}, Path=IsEnabled}"
Value="False">
<Setter TargetName="content"
Property="Visibility"
Value="Hidden" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You can do something with triggers:
Try setting the ItemTemplate to an empty DataTemplate when the box is disabled. This will affect the rendering of the selected item and therefore hide it.
Another simple but not very nice solution would be to set the foreground color to the same as a background color.
I believe you can do this with a Style, rather than redefining the control template. Use a Trigger on the IsEnabled property to set the text shown in the ComboBox. Altering the SelectedItem would be my first approach, but since you don't want to do that, you may find success setting the DisplayMemberPath. Something like this (untested)...
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Trigger.Setters>
<Setter Property="DisplayMemberPath" Value="{x:Null}"/>
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
Here's a style that does what you want. It employs a technique that I use all the time: a grid that contains multiple versions of the control, and data triggers that ensure that only one version is visible at any one time.
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<DockPanel>
<CheckBox x:Name="IsActive" DockPanel.Dock="Left"/>
<Grid>
<ComboBox
ItemsSource="{TemplateBinding ItemsSource}"
SelectedItem="{TemplateBinding SelectedItem}"
SelectedIndex="{TemplateBinding SelectedIndex}"
SelectedValue="{TemplateBinding SelectedValue}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=IsActive, Path=IsChecked}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
<ComboBox>
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=IsActive, Path=IsChecked}" Value="False">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</Grid>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.Style>
This preserves the selected item, selected index, and selected value, just as you want. In fact, it does this a little too well; there's not actually a way of telling that the user deactivated the combo box, since there's no property on ComboBox that exposes this information. I'd probably actually implement this as a custom control derived from ComboBox that exposed the value of the check box as an IsActive property. There are lots of other ways to do it.

Databinding to XML in a DataTrigger in WPF

In a WPF application, I have correctly bound a DataTemplate to an XML node that looks like:
<answer answer="Tree", correct="false" score="10" />
In my application, I have a TextBlock with the answer in it. At first, I want it invisible, but when the correct attribute in the XML file changes to "true", it must become visible.
My DataTemplate is hooked up correctly, because everything else works. For example, if I change the answer attribute in the XML file (just for testing), it changes in my WPF view. But I'm having troubles with the visibility. This is my XAML:
<TextBlock Text="{Binding XPath=#answer}" Visibility="Hidden">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding XPath=#correct}" Value="true">
<Setter Property="TextBlock.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
I'm guessing the Databinding in the DataTrigger isn't working correctly. Anyone have a clue?
I have run into the same problem with databound ToggleButtons. Try removing the Visibility="False" and replacing it with another DataTrigger that handles the incorrect case.
I think the issue is that the Visibility property is hard-coded. Try setting the Visibility in the style:
<TextBlock Text="{Binding XPath=#answer}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding XPath=#correct}" Value="true">
<Setter Property="TextBlock.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Sure, it works if you give a specific else case instead of just false. As in my case, it was {x:Null} and value. So when its value to bind is present, it will be true and TextBlock.Visibilty will be set using setters value and when binding path does not have any value inside it, i.e. null in my case, its simply {x:Null} :)

TextBlock Text property can't be set via style trigger if non-empty - why?

The XAML below does not work (the text does not change when mousing over):
<Window.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Text" Value="hover"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBlock Text="original"/>
</Grid>
But, if the Text attribute is missing:
<Grid>
<TextBlock/>
</Grid>
The text does change on mouse over. Anybody knows the theory behind this?
It's a DependencyProperty precedence issue, when you actually set the property as in:
<TextBlock Text="original"/>
that takes precedence over the value set in the trigger.
see
http://msdn.microsoft.com/en-us/library/ms743230.aspx

Resources