开发者

Using the same style in many listviews

开发者 https://www.devze.com 2022-12-24 04:43 出处:网络
I want to use the same style in many listviews. And in my style i have defined the gridview columns also.

I want to use the same style in many listviews. And in my style i have defined the gridview columns also.

But when i try to run, it throws an exception:

View can't be shared by more than one ListView.

How can i solve this?


XAML:

<Style x:Key="articleList" TargetType="{x:Type ListView}">
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
<Setter Property="ListView.ItemsSource" Value="{Binding}"/>
<Setter Property="ListView.View">
    <Setter.Value>
        <GridView>
            <GridViewColumn Header="Subject" Width="300">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Subject}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Size" Width="75">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding SizeFormatted}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </开发者_如何学PythonGridViewColumn>
            <GridViewColumn Header="Poster" Width="175">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Poster}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Age" Width="75">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding AgeFormatted}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </Setter.Value>
</Setter>


Add the x:Shared property to your GridView resource. Check out the GridView resource in this example.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Window1"
        x:Name="Window"
        Title="Window1"
        Width="640" Height="480">
  <Window.Resources>
    <GridView x:Key="ViewBase1" x:Shared="False">
      <GridViewColumn Header="Blah1" Width="70"/>
      <GridViewColumn Header="Blah2" Width="70"/>
      <GridViewColumn Header="Blah3" Width="70"/>
    </GridView>
  </Window.Resources>

  <Grid x:Name="LayoutRoot">
    <ListView Margin="0,0,0,120"    View="{DynamicResource ViewBase1}" />
    <ListView Margin="272,0,91,120" View="{DynamicResource ViewBase1}" />
  </Grid>
</Window>
0

精彩评论

暂无评论...
验证码 换一张
取 消