我有一个显示链接按钮的 ListView。我只需要链接按钮来显示“FileAddress”字符串是否有值。否则,它应该显示一个带有 Title 的简单 TextBlock。
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Style="{StaticResource LinkButton}" Height="23" Content="{Binding Path=Title}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
任何想法,将不胜感激。
TIA。
最佳答案
我认为“gridview”是 wpf 的一个简单组件。进行这种类型的更改有点笨拙。
您可以采用三种不同的方法:
1) 使用 GridViewColumn.CellTemplateSelector (http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector)
虽然不喜欢=(
2) 创建一个根据需要运行的 UserControl。
看起来合适且相对容易
3) 通过触发器更改按钮样式:
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="Content" Value="{x:Null}">
<!-- Changes -->
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
或者
<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>
使用触发器时有一些限制,有时您最终不得不为按钮创建一个新模板,如果您知道使用 xaml 相对容易且简单。
示例(我在这里有一个按钮,我正在更改他的图像):
<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>
关于wpf - ListView 基于绑定(bind)值的不同控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199999/