Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View can't be shared by more than one Listview

I have a problem which it seemed, many other people had before. I try to use a style, which contains a GridView by several controls.

I searched the web for answers and got two solutions:

  • Change StaticResource with DynamicResource
  • Use x:shared="False" on the GridView

I tried both solutions (even at the same time) but the error still appears as soon as I'm applying the style to a second control.

The style looks like this

<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>

and here the 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>

What can I do?

like image 793
StefanG Avatar asked Sep 06 '25 05:09

StefanG


1 Answers

It sounds like you only have one single instance of CustomGridView created, and you are trying to assign it to multiple places, which is causing the exception.

According to the 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.

I'm not exactly sure what it means to be a Compiled ResourceDictionary, but you could try changing the Build Action to Page instead of Resource, like this answer suggests.

If you can't get x:Shared working, then perhaps consider making SuggestionsView a ContentTemplate or DataTemplate instead so each use of it will create it's own 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>
like image 54
Rachel Avatar answered Sep 10 '25 10:09

Rachel