0

I am using following code -

<Label Grid.Row="0" Content="{Binding MyColor}">
  <Label.Background>
    <SolidColorBrush Color="{Binding MyColor}"></SolidColorBrush>
  </Label.Background>
</Label>

<Grid Grid.Row="1" Grid.Column="0">
  <ScrollViewer Name="MyScroll"
                Template="{DynamicResource MyScrollViewerControlTemplate}">
    <ListView Name="List1"
              BorderThickness="0"
              SelectedItem="{Binding Path=SelectedElement}"
              ItemsSource="{Binding Path=Elements}"
              Background="{StaticResource aColor}">

      <ListView.Resources>
        <ControlTemplate x:Key="SelectedTemplate"
                         TargetType="ListViewItem">
          <Border Cursor="Hand">
            <Border.Background>
              <SolidColorBrush Color="{Binding Path=myColor}">
              </SolidColorBrush>
            </Border.Background>
            <TextBlock Text="Test" />
          </Border>
        </ControlTemplate>
      </ListView.Resources>
    </ListView>
  </ScrollViewer>
</Grid>

Here when I am using property myColor to set Background color for label, It is working fine but when I am doing the same in ListView it is not working.

What have I missed. Please suggest.

4

1 回答 1

0

我找到了答案。我在 ListView 中将 Elements 称为我的绑定源,它不能直接访问 myColor 属性。我需要在可视化树中向上搜索 DataContext 并需要引用它。

我使用了以下代码 -

<Border.Background>
  <SolidColorBrush
    Color="{Binding Path=DataContext.myColor, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}}}">
  </SolidColorBrush>
</Border.Background>

我成功地获得了财产。

于 2013-04-24T06:28:56.853 回答