gpt4 book ai didi

wpf - View 不能被多个 Listview 共享

转载 作者:行者123 更新时间:2023-12-04 14:30:59 25 4
gpt4 key购买 nike

我有一个问题,以前很多人都遇到过。我尝试使用一种样式,其中包含由多个控件组成的 GridView。

我在网上搜索了答案,得到了两个解决方案:

  • 用动态资源改变静态资源
  • 在 GridView 上使用 x:shared="False"

我尝试了这两种解决方案(甚至同时尝试),但是当我将样式应用于第二个控件时错误仍然出现。

样式是这样的

<Style x:Key="MyCustomStyle" TargetType="{x:Type MyUserControl}" BasedOn="{StaticResource MyUserControlBaseStyle}">
<Setter Property="Watermark" Value="{x:Static LabelResources.Caption}"/>
<Setter Property="ErrorText" Value="{Binding Converter={StaticResource FormatStringConverter}, ConverterParameter={x:Static LabelResources.Caption}}"/>
<Setter Property="LookupTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type LookupResult}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="0,0,2,0"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>

<Setter Property="SuggestionsView" Value="{DynamicResource CustomGridView}"/>
</Style>

这里是 GridView

<GridView x:Key="CustomGridView" x:Shared="False">
<GridViewColumn Header="{x:Static resources:LabelResources.Name}" Width="Auto" HeaderContainerStyle="{DynamicResource GridViewHeaderStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>

我能做什么?

最佳答案

听起来您只创建了一个 CustomGridView 实例,并且您试图将它分配到多个位置,这导致了异常。

根据MSDN page on x:Shared

In WPF, x:Shared is only valid under the following conditions:

  • The ResourceDictionary that contains the items with x:Shared must be compiled. The ResourceDictionary cannot be within loose XAML or used for themes.

  • The ResourceDictionary that contains the items must not be nested within another ResourceDictionary. For example, you cannot use x:Shared for items in a ResourceDictionary that is within a Style that is already a ResourceDictionary item.

我不太确定编译的 ResourceDictionary 是什么意思,但您可以尝试将构建操作更改为页面而不是资源,例如 this answer suggests .

如果你不能让 x:Shared 正常工作,那么或许可以考虑将 SuggestionsView 变成 ContentTemplateDataTemplate,这样每次使用它将创建它自己的 GridView。

<ContentTemplate x:Key="CustomGridView">
<GridView>
<GridViewColumn Header="{x:Static resources:LabelResources.Name}" Width="Auto" HeaderContainerStyle="{DynamicResource GridViewHeaderStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ContentTemplate>

关于wpf - View 不能被多个 Listview 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023694/

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