在资源字典中设置列表视图数据模板的样式

时间:2014-11-09 15:05:36

标签: c# wpf

我在资源字典中有以下样式:

 <!--  A style for list views that only display images  -->
    <Style x:Key="ImageListViewStyle" TargetType="{x:Type ListView}">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="Background" Value="{StaticResource FandFCream}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="SelectionMode" Value="Single" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <!--  Use a wrap panel so that the items appear side by side rather than stacked vertically  -->
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <!--  Changes the colour of a selected item and keeps that colour when the focus is lost  -->
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListViewItem}">
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{StaticResource Green}" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{StaticResource Green}" />
                    </Style.Resources>
                </Style>
            </Setter.Value>
        </Setter>
        <!--  Change the item template  -->
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>

                    <!--  A border around each item  -->
                    <Border Name="ItemBorder" Style="{StaticResource FandFItemBorderStyle}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我想要的是能够在数据模板中的边框中显示图像。我该怎么做才能在多个控件中重用这个样式但是能够将图像源绑定到视图模型?

例如,我认为如果风格如下:

<DataTemplate>
      <!--  A border around each item  -->
      <Border Name="ItemBorder" Style="{StaticResource FandFItemBorderStyle}">
           <ContentPresenter />
      </Border>
</DataTemplate>

然后我可以将图像对象分配给该内容控件中的列表视图,但我不确定如何实际执行此操作。

  • WPF
  • C#
  • Visual Studio 2012

0 个答案:

没有答案