WPF-无法绑定到DependencyProperty,无法正常工作

WPF-无法绑定到DependencyProperty,无法正常工作,wpf,button,binding,dependency-properties,isenabled,Wpf,Button,Binding,Dependency Properties,Isenabled,我在我的窗口中定义了一个依赖项属性,如下所示: public static readonly DependencyProperty IsGenericUserProperty = DependencyProperty.Register("IsGenericUser", typeof (bool), typeof (MainWindow)); public bool IsGenericUser { get { return (bool) GetValue(IsGenericUserProp

我在我的窗口中定义了一个依赖项属性,如下所示:

public static readonly DependencyProperty IsGenericUserProperty = DependencyProperty.Register("IsGenericUser", typeof (bool), typeof (MainWindow));
public bool IsGenericUser
{
    get { return (bool) GetValue(IsGenericUserProperty); }
    set { SetValue(IsGenericUserProperty, value); }
}
在窗口的构造函数上,我设置了保存按钮的容器的数据上下文:

QuickListButtonsStackPanel.DataContext = this;
我正在将dependency属性绑定到按钮的IsEnabled属性:

<Button IsEnabled="{Binding IsGenericUser}" .../>

启动时IsGenericUser为true,因此该按钮处于启用状态。当我将IsGenericUser设置为false时,按钮将被禁用。但是,如果我再次将IsGenericUser设置为true,则该按钮不会发生任何变化,它将保持禁用状态。 我做错了什么

谢谢

编辑: 这是我正在使用的按钮样式。此样式导致问题(如果按钮没有自定义样式,则可以正常工作):


如何将属性设置为False/True?如果我按原样复制您的代码,它将完美工作。必须有其他一些你可能不希望影响它的事情发生,比如按钮上的动画或者清除绑定的事情。您是否可以发布更多的代码来帮助澄清这一点

下面是我测试的代码:

<Window x:Class="WpfApplication6.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="300">
<Grid>
    <StackPanel x:Name="QuickListButtonsStackPanel">
        <Button IsEnabled="{Binding IsGenericUser}"
                Content="Bound Button" />
        <Button Content="Change Binding"
                Click="Button_Click" />
    </StackPanel>
</Grid>
编辑: 您还可以添加一个文本框,查看它是否正常工作

<Button x:Name="uiButton"
        IsEnabled="{Binding IsGenericUser}"
        Style="{StaticResource BlackButtonStyle}"
        Content="Bound Button"/>
<TextBlock Text="{Binding ElementName=uiButton, Path=IsEnabled}" />

看起来问题只在于样式的故事板,如果你加上它,它是否仍然显示IsEnabled在不应该的情况下为false?

试试看

<Button IsEnabled={Binding Path=IsGenericUser}" ... />
1)创建了一个名为DisableDeactivating的新情节提要,并设置FillBehavior=“Stop”(尼古拉斯的建议)

2) 然后,在IsEnabled=false触发器的Trigger.ExitActions中添加了一个BeginStoryboard用于禁用停用

如果有人遇到相同的问题-很可能您的DataContext未设置或动态更改为另一个对象。它发生在一些包含大量不同控件的模板中。

抱歉,在使用上述所有内容的测试项目中,所有这些都可以正常工作。其他一些特定于我的项目的东西可能是错误的。目标公司的那个按钮还有其他东西吗?您是否通过代码设置任何属性或对其应用任何动画?是的,我将动画应用于按钮的样式。这可能就是问题的原因。如果我不使用按钮样式,一切正常。你的按钮有命令吗?如果命令的CanExecute返回false(或者您没有通知CanExecute已更改),则该按钮将被禁用。否,没有命令。谢谢,“这”是现在的课。因此,将“this”分配给DataContext将把当前类分配给它(在我的例子中是我的窗口)。窗口有DP,所以是的,它是正确的。谢谢。你是对的-我在按钮上使用的样式导致了这种奇怪的行为。我在问题中包括了这种风格。我在风格上遗漏了什么?谢谢!这就是我所做的:1)创建了一个名为DisableDeactivating的新故事板,并设置FillBehavior=“Stop”2)然后,在触发器中添加了一个BeginStoryboard用于DisableDeactivating。IsEnabled=false触发器的ExitActions。
<Button x:Name="uiButton"
        IsEnabled="{Binding IsGenericUser}"
        Style="{StaticResource BlackButtonStyle}"
        Content="Bound Button"/>
<TextBlock Text="{Binding ElementName=uiButton, Path=IsEnabled}" />
<Button IsEnabled={Binding Path=IsGenericUser}" ... />