Reputation: 1433
I have a ListView that displays a link button. I need the link button only to display if the 'FileAddress' string has a value. It should display a simple TextBlock with the Title otherwise.
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Style="{StaticResource LinkButton}" Height="23" Content="{Binding Path=Title}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
Any ideas would be appreciated.
TIA.
Upvotes: 1
Views: 505
Reputation: 3361
I consider the "gridview" a simple component of wpf .. Where is a bit clumsy to carry out this type of change.
You can make three different approaches:
1) Use GridViewColumn.CellTemplateSelector ( http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector )
Although I do not like = (
2) Create a UserControl that behaves as desired.
It seems appropriate and relatively easy
3) Change the button style through a trigger:
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="Content" Value="{x:Null}">
<!-- Changes -->
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
or
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<!-- Template -->
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Content" Value="{x:Null}">
<!-- Changes -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
There are limitations when using the trigger, sometimes you end up having to create a new template for the button, if you know messing with xaml is relatively easy and simple to do.
Sample (I have a button here and I'm changing his image):
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Image x:Name="imgBackground" Source="{StaticResource UpArrowImageNormal}" Stretch="None"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="imgBackground"
Property="Source" Value="{StaticResource UpArrowImageIsPressed}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="imgBackground" Property="Source" Value="{StaticResource UpArrowImageDisabled}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
Upvotes: 2
Reputation: 1670
You could use the following converter
[ValueConversion(typeof(Object), typeof(Visibility))]
public class NullVisibilityConv : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return Visibility.Collapsed;
else return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
You can then bind to the control using the following xaml
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Style="{StaticResource LinkButton}" Height="23" Content="{Binding Path=Title}"
Visibility="{Binding FileAddress, Converter={StaticResource NullToVisConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
You also need to declare the converter as a resource somewhere in your xaml
<converters:NullVisibilityConv x:Key="NullToVisConverter" />
An advantage of this approach is that once you declare the converter in your code you can then re-use it again on other bindings without adding triggers or any other complex code, just use that converter on your visibility binding
Upvotes: 0