我正在尝试根据数据触发器更改文本块的文本。 datatrigger看起来像这样:
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty}" Value="true">
<Setter Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty}" Value="false">
<Setter Property="Text" Value="{Binding Path=NameWithoutExtension}" />
</DataTrigger>
</Style.Triggers>
</Style>
此代码位于“MyView”类的xaml文件中。此类的cs文件包含以下代码:
public static readonly DependencyProperty ShowFileExtensionsProperty =
DependencyProperty.Register("ShowFileExtensions", typeof(bool), typeof(MyView), new UIPropertyMetadata(false));
public bool ShowFileExtensions
{
get { return (bool)GetValue(ShowFileExtensionsProperty); }
set
{
SetValue(ShowFileExtensionsProperty, value);
updateViewCollection();
}
}
textblock显示在listview中,已绑定到对象列表,这些对象包含“Name”和“NameWithoutExtension”属性。
问题是文本块中的文本始终为空白。不知何故,数据触发器永远不会被触发。知道数据触发器有什么问题吗?
PS:完整的代码如下
<UserControl x:Class="com.xploreplus.Filedrops.explorer.listview.FiledropsFileList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:l="clr-namespace:com.xploreplus.Filedrops.explorer.listview.views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style x:Key="Extension" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="true">
<Setter Property="Text" Value="brol2" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="false">
<Setter Property="Text" Value="blabla" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<ListView Name="viewComponent">
<ListView.View>
<GridView>
<GridViewColumn Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="{Binding Path=Icon}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource Extension}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Extension" DisplayMemberBinding="{Binding Path=Extension}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
答案 0 :(得分:0)
由于文本块显示在listview中,因此如果没有显式设置,则textblock的DataContext将是ItemDataContext。这就是你的绑定不起作用的原因。您必须将relativesource提供给绑定
尝试更新
等绑定<DataTrigger Binding="{Binding Path=ShowFileExtensions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="true">
使用{x:Type Control},假设这是您的视图类型,否则您必须更改为您的视图类型。
希望这有帮助
由于