XAML DependencyProperty使用Bool更改背景颜色

wgx48brx  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(137)

我试图更改自定义控件矩形的背景颜色。如果我的属性(bool)为setSelected==true,则颜色应为Red。如果为setSelected==false,则颜色应为Transparent
如何更改Rectangle的装订背景颜色?
通用:

<Style TargetType="{x:Type local:IdnStatusRect}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:IdnStatusRect}">
                <Border Background="Transparent"
                        BorderBrush="Purple"
                        BorderThickness="0">
                    <DockPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="15" Height="15" Background="Transparent">
                        <Grid>
                            <Rectangle Width="15" Height="15" Fill="Transparent" Stroke="#97CDEB" StrokeThickness="1"/>
                            <Grid.Background>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <GradientStop Color="{Binding Path=BackgroundColor, RelativeSource={RelativeSource TemplatedParent}}" Offset="0" />

                                </LinearGradientBrush>

                            </Grid.Background>

                        </Grid>
                    </DockPanel>
                </Border>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

状态矩形:

public class IdnStatusRect : Control
{
    public bool setSelected
    {
        get { return (bool)GetValue(setSelectedPropert); }
        //  set { SetValue(RectPropert1, Application.Current.FindResource(value.ToString())); }
        set { SetValue(setSelectedPropert, value); }
    }
    
    public static readonly DependencyProperty setSelectedPropert =
        DependencyProperty.Register("setSelected", typeof(bool), typeof(IdxButton), new UIPropertyMetadata(false));

    public Color BackgroundColor
    {
        get { return (Color)GetValue(setSelectedPropert); }
        set { SetValue(BackgroundColorProperty, FindResource("TextColor1")); }
    }

    public static readonly DependencyProperty BackgroundColorProperty =
                DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(IdxButton), new PropertyMetadata((sender, args) => {
                    System.Diagnostics.Debug.WriteLine("Set bg col");
                }));

    static IdnStatusRect()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(IdnStatusRect), new FrameworkPropertyMetadata(typeof(IdnStatusRect)));
    }
xkftehaa

xkftehaa1#

您可以将触发器添加到监视SetSelected属性的控件模板中。您需要将x:Name分配给Rectangle,以便在setter中使用TargetName引用它。

<ControlTemplate TargetType="{x:Type local:IdnStatusRect}">
   <Border ...>
      <DockPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="15" Height="15" Background="Transparent">
         <Grid>
            <Rectangle x:Name="SelectionRectangle" Fill="Transparent" .../>
            <!-- ..other markup. -->
         </Grid>
      </DockPanel>
   </Border>
   <ControlTemplate.Triggers>
      <Trigger Property="SetSelected" Value="True">
         <Setter TargetName="SelectionRectangle" Property="Fill" Value="Red"/>
      </Trigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

另一个选项是使用具有相对源绑定的Rectangle的样式。

<Rectangle Width="15" Height="15" Stroke="#97CDEB" StrokeThickness="1">
   <Rectangle.Style>
      <Style TargetType="{x:Type Rectangle}">
         <Setter Property="Fill" Value="Transparent"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding SetSelected, RelativeSource={RelativeSource AncestorType={x:Type local:IdnStatusRect}}}" Value="True">
               <Setter Property="Fill" Value="Red"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Rectangle.Style>
</Rectangle>

请考虑将依赖项属性与约定保持一致。

  • 属性应以Pascal大小写形式定义(以大写字母开头)
  • 相依性属性名称必须以Property结尾。
  • 所有者类型IdxButton与控件类型IdnStatusRect不对应。
  • 依赖项属性的 Package 属性不能包含使用相应方法获取和设置值之外的其他代码,因为可以使用依赖项属性本身设置这些代码,从而绕过自定义代码。如果需要,请改用依赖项属性定义中的属性更改回调。
    ***提示:**使用nameof而不是硬编码字符串引用 Package 属性名称.
public bool SetSelected
{
   get => (bool)GetValue(SetSelectedProperty);
   set => SetValue(SetSelectedProperty, value);
}

public static readonly DependencyProperty SetSelectedProperty =
   DependencyProperty.Register(nameof(SetSelected), typeof(bool), typeof(IdnStatusRect), new UIPropertyMetadata(false));

相关问题