如何在ItemsControl中实现DependencyProperty

时间:2019-07-17 16:21:29

标签: c# wpf

我有一个用户控件(DeckList),它由一个ItemsControl组成,该控件仅在网格中并排显示3个Label。

<ItemsControl x:Class="OpponentDeck.DeckList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:OpponentDeck"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

  <ItemsControl.ItemContainerStyle>
    <Style>
      <Setter Property="FrameworkElement.Margin" Value="0,0,0,0" />
    </Style>
  </ItemsControl.ItemContainerStyle>

  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="20"/>
          <ColumnDefinition Width="100"/>
          <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>

        <!--<Rectangle Grid.Column="0" Fill="{Binding Card.Background}" Height="34" Width="217" />-->
        <Label x:Name="lblCost" Grid.Column="0" Content="{Binding Card.Cost}" Foreground="LimeGreen" HorizontalContentAlignment="Center" Background="Black" FontSize="12" FontFamily="Tekton Pro" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center"/>
        <Label x:Name="lblCard" Grid.Column="1" Content="{Binding Card.Name}" Foreground="{Binding TextColor, ElementName=lblCard}" HorizontalContentAlignment="Center" Background="Black" FontSize="12" FontFamily="Tekton Pro" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Left"/>
        <Label x:Name="lblQty" Grid.Column="2" Content="{Binding Qty}" Foreground="LimeGreen" HorizontalContentAlignment="Center" Background="Black" FontSize="12" FontFamily="Tekton Pro" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center"/>
      </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

该用户控件(DeckList)在另一个添加到主窗口的UserControl(UI)中使用。该用户控件由一个并排带有2个DeckList的堆栈面板组成。在定义UI.xaml时,我想通过设置属性来指定标签的颜色。我有这个标记来做

    <controls1:DeckList TextColor="LimeGreen"  x:Name="playableCards" Width="150"></controls1:DeckList>

DeckList的代码隐藏具有DependencyProperty和属性Get / Set

public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(SolidColorBrush), typeof(DeckList), new PropertyMetadata(new SolidColorBrush(Colors.HotPink)));
        public System.Windows.Media.Brush TextColor {
            get { return (System.Windows.Media.Brush)GetValue(TextColorProperty); }
            set { SetValue(TextColorProperty, value); }
        }

我很确定问题与绑定有关,但是对于我的一生,我不知道如何使它起作用。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你有

<Label x:Name="lblCard" Foreground="{Binding TextColor, ElementName=lblCard}" .../>

由于Label没有TextColor属性而无法使用。

为了使用DeckList控件的TextColor属性,请使用

Foreground="{Binding TextColor, RelativeSource={RelativeSource AncestorType=ItemsControl}}"