我刚刚开始用 WPF 测试水,我正在尝试将扩展器的扩展属性和列表视图项的选定属性绑定在一起,以便在选择列表视图项时扩展器扩展或沿着另一条路尝试将列表视图项设置为在扩展器展开时选择
到目前为止我有
<ListView HorizontalAlignment="Stretch" Name="listView1" VerticalAlignment="Stretch" SelectionMode="Single" >
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
</Style>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<Expander>
<TextBlock Text="{Binding Name}"></TextBlock>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在绑定中引用扩展器。任何帮助或朝正确方向的推动将不胜感激。
谢谢
好..
你不能用它自己的模板连接 listboxitem ......因为基本上他们不知道......这在这里不起作用:
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=expanderHeader,Mode=OneWay}" Value="True">
<Setter Property="IsSelected" value="True"/>
</ DataTrigger>
</ Style.Triggers>
</ Style>
Run Code Online (Sandbox Code Playgroud)
你也不能触发扩展器的触发器,因为设置器不接受绑定..
<Expander.Style>
<Style TargetType="Expander">
<Style.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter Property="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=IsSelected}" Value="True"/>
</ Trigger>
</ Style.Triggers>
</ Style>
</ Expander.Style>
Run Code Online (Sandbox Code Playgroud)
答案是这样的:
<ListBox.ItemTemplate>
<DataTemplate>
<Expander x:Name="expanderHeader" IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}">
<!-- Content -->
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以根据您的需要使用绑定模式 = OneWayToSource ..
| 归档时间: |
|
| 查看次数: |
6422 次 |
| 最近记录: |