0

我有一个ObservableCollection<Foo> fooList, 和下面的代码 a MasterDetailsView,这只是一个花哨的ListView

<controls:MasterDetailsView
            Grid.Row="1"
            x:Name="MasterDetailsViewControl"
            ItemsSource="{x:Bind fooList}"
            SelectedItem="{x:Bind Selected, Mode=OneWay}"
            ItemTemplate="{StaticResource ItemTemplate}"
            DetailsTemplate="{StaticResource DetailsTemplate}"
            NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"
            BorderBrush="Transparent">
    <controls:MasterDetailsView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Background">
                <Setter.Value>
                    <Binding Path="PathToFooBrushGoesHere" />
                </Setter.Value>
            </Setter>
        </Style>
    </controls:MasterDetailsView.ItemContainerStyle>
</controls:MasterDetailsView>

每个foo都有一个单独的属性foo.Brush,我想用它来绘制背景ListViewItem。我无法弄清楚我需要输入什么PathToFooBrushGoesHere才能ListViewItem访问foo具有Brush.

我有一个想法,通过设置不同的 ,它可能是可行的DataContext,但我无法弄清楚我如何能够访问样式DataContextItemTemplate的。ListViewItem

应该如何进行这种造型?

4

1 回答 1

0

Brush您可以通过将属性绑定到ItemTemplateof来实现此效果MasterDetailsView

首先,将以下更改MasterDetailsView.ItemContainerStyle为将VerticalContentAlignment和的HorizontalContentAlignment属性设置ListViewItemStretch

<controls:MasterDetailsView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</controls:MasterDetailsView.ItemContainerStyle>

然后将Brush属性绑定到ItemTemplateof MasterDetailsView

<DataTemplate x:Key="ItemTemplate" x:DataType="local:Foo">
    <Grid Background="{x:Bind Brush}">
        <TextBlock Text="ItemTemplate"/>
    </Grid>
</DataTemplate>
于 2018-03-19T08:10:43.997 回答