gpt4 book ai didi

WPF 在 Listview 中展开特定的扩展器

转载 作者:行者123 更新时间:2023-12-02 06:38:43 25 4
gpt4 key购买 nike

我有一个 ListView ,其中的组样式,我已经定义了一个扩展器(下面的代码),我以编程方式将许多项目添加到 ListView 中,这些项目被添加到适当的扩展器中,如果新项目不存在扩展器,它会得到动态创建。

<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" >
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>

所以需要做的是,当添加一个新项目时,焦点应该转移到那个项目上,扩展器应该在折叠所有其他项目的同时展开......

最佳答案

我想扩展之前的答案——我在 MVVM 场景中使用它,并且遇到了与上面提到的 Billy 相同的错误

" A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."

我最终修改了转换器和 XAML(DataContext.CurrentItem 是引用 View 模型中当前项目所需的任何内容的占位符):

public class MultiBindingItemsEqualConverter : IMultiValueConverter
{
#region IMultiValueConverter Members

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool valuesEquals = values[0].Equals(values[1]);
return valuesEquals;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

#endregion
}

<Expander ...>
<Expander.IsExpanded>
<MultiBinding
Mode="OneWay"
Converter="{StaticResource MultiBindingItemsEqualConv}">
<Binding
RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}"
Path="SelectedItem"></Binding>
<Binding
RelativeSource="{RelativeSource FindAncestor, AncestorType=ListView, AncestorLevel=1}"
Path="DataContext.CurrentItem"></Binding>
</MultiBinding>
</Expander.IsExpanded>
</Expander>

关于WPF 在 Listview 中展开特定的扩展器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12319398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com