Wpf 为什么不更改依赖项属性?

Wpf 为什么不更改依赖项属性?,wpf,Wpf,这是我的自定义控制代码 public partial class FlatImageButton : ContentControl { public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool?), typeof(FlatImageButton), new FrameworkPropertyMetadata(false,

这是我的自定义控制代码

public partial class FlatImageButton : ContentControl
{
    public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool?), typeof(FlatImageButton), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsActiveChanged)));
    public bool IsActive
    {
        get => (bool)GetValue(IsActiveProperty);
        set => SetValue(IsActiveProperty, value);
    }

    public FlatImageButton() : base()
    {
        InitializeComponent();
    }

    private static void IsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FlatImageButton button = (FlatImageButton)d;

        button.IsActive = (bool) e.NewValue;
    }
}
这是自定义控件xaml代码。
i活动
属性更改
背景
属性

<ContentControl ...>

    <Button x:Name="_Control" Focusable="False">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Rectangle Fill="Transparent" />
                    <Border Padding="{Binding ElementName=_Root, Path=Padding}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Image Width="24" Height="24" Source="{Binding ElementName=_Root, Path=Image}" />
                            <TextBlock Name="Text" Text="{Binding ElementName=_Root, Path=Text}" Foreground="{Binding ElementName=_Root, Path=Fill}" Margin="10,0,0,0" VerticalAlignment="Center" />
                        </StackPanel>
                        <Border.Style>
                            <Style TargetType="Border">
                                <Setter Property="Background" Value="{Binding ElementName=_Root, Path=Background}" />
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="{Binding ElementName=_Root, Path=Foreground}" />
                                    </Trigger>
                                    <DataTrigger Binding="{Binding ElementName=_Root, Path=IsActive}" Value="True">
                                        <Setter Property="Background" Value="{Binding ElementName=_Root, Path=Foreground}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
</ContentControl>

尝试更改另一个控件中的“IsActive”时出现问题

<common:FlatImageButton Tag="{x:Static enum:Coin.ETHEREUM}" x:Name="_CEthereum" Cursor="Hand" Padding="10, 10" Text="{Binding ViewModel.Coin,ElementName=_Root}" Background="#EEE" Foreground="#DDD" Fill="#333" Image="pack://application:,,,/Resources/ethereum.png">
  <common:FlatImageButton.Style>
    <Style TargetType="common:FlatImageButton">
      <Setter Property="IsActive" Value="False" />
      <Style.Triggers>
        <DataTrigger Binding="{Binding ViewModel.Coin, ElementName=_Root}" Value="ETHEREUM">
          <Setter Property="IsActive" Value="True" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </common:FlatImageButton.Style>
</common:FlatImageButton>

为什么不换房子呢

详情:

  • ViewModel。硬币价值始终为以太坊

实际上不需要属性更改回调,您可以将其删除,只需将属性元数据保留为
新的FrameworkPropertyMetadata(false)


属性更改回调是值更改后需要执行的逻辑,建议不要在回调函数中更改属性值。

尝试将IsActive更改为common::FlatImageButton.IsActive


您还可以看到

我已经删除了新的FrameworkPropertyMetadata。然而,它不起作用。虽然这当然是真的,但它并没有回答这个问题。它应该是一个评论,不是这样的。我需要在另一个控件中使用触发器。这里有两个问题:1。如何验证“IsActive”不起作用?(例如,通过检查背景是否改变)2。元素名“_Root”代表什么?一个小提示:调试绑定错误时,您可以参考“输出”窗口。文本绑定,即
Text=“{binding ViewModel.Coin,ElementName=\u Root}”
正在工作吗?尝试使用实际枚举值而不是字符串作为DataTrigger的值,就像您对标记属性所做的那样。
Text=“{Binding ViewModel.Coin,ElementName=_Root}”正在工作。好的,您是否尝试了
value=“{x:Static enum:Coin.ETHEREUM}”而不是
value=“ETHEREUM”
?是的。它不起作用了(不清楚“不工作”在这里的真正含义是什么。您还没有显示当IsActive属性更改时应该发生什么。您如何真正知道它没有更改?