WPF - IsEnabled绑定到的DependencyProperty工作不正常 [英] WPF - IsEnabled Binding to DependencyProperty not working properly

查看:910
本文介绍了WPF - IsEnabled绑定到的DependencyProperty工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的窗口,如下定义的依赖项属性:

 公共静态只读的DependencyProperty IsGenericUserProperty = DependencyProperty.Register(IsGenericUser的typeof(布尔)的typeof(主窗口));
公共BOOL IsGenericUser
{
    {返回(布尔)的GetValue(IsGenericUserProperty); }
    集合{的SetValue(IsGenericUserProperty,值); }
}

在我的窗户的构造我设置容器保持按钮的数据上下文:

  QuickListButtonsStackPanel.DataContext =这一点;

我的依赖属性绑定到一个按钮的IsEnabled属性:

 <按钮IsEnabled ={结合IsGenericUser}... />

在启动IsGenericUser是真实的,所以按钮将被启用。当我设置IsGenericUser为false,该按钮被禁用。但是,如果我做IsGenericUser实现再没有任何反应的按钮,它仍然是禁用的。
我在做什么错了?

谢谢!

编辑:
这里是我的按钮使用样式。这种风格导致问题(如果按钮没有自定义样式,它工作正常):

 <风格X:键=BlackButtonStyle的TargetType ={X:类型按钮}>
    < setter属性=模板>
        < Setter.Value>
            <的ControlTemplate的TargetType ={X:类型按钮}>
                < ControlTemplate.Resources>
                    <情节提要X:键=MouseOverActivati​​ng>
                        < ColorAnimationUsingKeyFrames的BeginTime =00:00:00Storyboard.TargetName =矩形Storyboard.TargetProperty =。(Shape.Fill)(GradientBrush.GradientStops)[1](GradientStop.Color)>
                            < SplineColorKeyFrame KeyTime =00:00:00VALUE =#FF2F2F2F/>
                            &所述; SplineColorKeyFrame KeyTime =00:00:00.1270000值=#FF2391FF/>
                        < / ColorAnimationUsingKeyFrames>
                    < /故事板>
                    <情节提要X:键=MouseOverDeactivati​​ng>
                        < ColorAnimationUsingKeyFrames的BeginTime =00:00:00Storyboard.TargetProperty =。(Shape.Fill)(GradientBrush.GradientStops)[1](GradientStop.Color)。Storyboard.TargetName =矩形>
                            < SplineColorKeyFrame KeyTime =00:00:00VALUE =#FF2391FF/>
                            &所述; SplineColorKeyFrame KeyTime =00:00:00.2200000值=#FF2F2F2F/>                        < / ColorAnimationUsingKeyFrames>
                    < /故事板>
                    <情节提要X:关键=pressActivati​​ng>
                        < ColorAnimationUsingKeyFrames的BeginTime =00:00:00Storyboard.TargetName =矩形Storyboard.TargetProperty =。(Shape.Fill)(GradientBrush.GradientStops)[1](GradientStop.Color)>
                            < SplineColorKeyFrame KeyTime =00:00:00VALUE =#FF2391FF/>
                            &所述; SplineColorKeyFrame KeyTime =00:00:00.1370000值=#FF48D6FF/>
                        < / ColorAnimationUsingKeyFrames>
                    < /故事板>
                    <情节提要X:关键=pressedDeactivati​​ngFillBehavior =停止>
                        < ColorAnimationUsingKeyFrames的BeginTime =00:00:00Storyboard.TargetProperty =。(Shape.Fill)(GradientBrush.GradientStops)[1](GradientStop.Color)。Storyboard.TargetName =矩形>
                            < SplineColorKeyFrame KeyTime =00:00:00VALUE =#FF48D6FF/>
                            &所述; SplineColorKeyFrame KeyTime =00:00:00.2370000值=#FF2391FF/>
                        < / ColorAnimationUsingKeyFrames>
                    < /故事板>
                    <情节提要X:键=DisableActivati​​ng>
                        < ColorAnimationUsingKeyFrames的BeginTime =00:00:00时间=00:00:00.0010000。Storyboard.TargetName =矩形Storyboard.TargetProperty =(Shape.Fill)(GradientBrush.GradientStops)[1](渐变停止。颜色)>
                            < SplineColorKeyFrame KeyTime =00:00:00VALUE =#FFA7A7A7/>
                        < / ColorAnimationUsingKeyFrames>
                    < /故事板>
                < /ControlTemplate.Resources>
                <网格和GT;
                    <矩形行程=透明半径=5半径=5×:NAME =矩形>
                        < Rectangle.Fill>
                            <一个LinearGradientBrush终点=0.5,1StartPoint可以=0.5,0>
                                &所述;渐变停止颜色=#FF000000偏移=0/>
                                &所述;渐变停止颜色=#FF2F2F2F偏移=1/>
                            < /一个LinearGradientBrush>
                        < /Rectangle.Fill>
                    < /矩形>
                    <内容presenter的Horizo​​ntalAlignment ={TemplateBinding Horizo​​ntalContentAlignment}VerticalAlignment ={TemplateBinding VerticalContentAlignment}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}RecognizesAccessKey =真OpacityMask ={X:空}/>
                    <矩形行程=透明半径=5半径=5×:NAME =WhiteGlow>
                        < Rectangle.Fill>
                            <一个LinearGradientBrush终点=0.5,1StartPoint可以=0.5,0>
                                &所述;渐变停止颜色=#5BFFFFFF偏移=0/>
                                <渐变停止颜色=#00FFFFFF偏移量=0.5/>
                            < /一个LinearGradientBrush>
                        < /Rectangle.Fill>
                    < /矩形>
                < /网格和GT;
                < ControlTemplate.Triggers>
                    <触发属性=IsCancelVALUE =FALSE/>
                    <的EventTrigger RoutedEvent =FrameworkElement.Loaded/>
                    <触发属性=IsFocusedVALUE =真>
                        < Trigger.ExitActions>
                            < BeginStoryboard故事板={StaticResource的MouseOverActivati​​ng}X:NAME =MouseOverActivati​​ng_BeginStoryboard2/>
                        < /Trigger.ExitActions>
                        < Trigger.EnterActions>
                            < BeginStoryboard故事板={StaticResource的MouseOverActivati​​ng}X:NAME =MouseOverActivati​​ng_BeginStoryboard1/>
                        < /Trigger.EnterActions>
                    < /触发>
                    <触发属性=IsDefaultedVALUE =真/>
                    <触发属性=IsMouseOverVALUE =真>
                        < Trigger.ExitActions>
                            < BeginStoryboard故事板={StaticResource的MouseOverDeactivati​​ng}X:NAME =MouseOverDeactivati​​ng_BeginStoryboard/>
                        < /Trigger.ExitActions>
                        < Trigger.EnterActions>
                            < BeginStoryboard故事板={StaticResource的MouseOverActivati​​ng}X:NAME =MouseOverActivati​​ng_BeginStoryboard/>
                        < /Trigger.EnterActions>
                    < /触发>
                    <触发属性=是pressedVALUE =真>
                        < Trigger.EnterActions>
                            < BeginStoryboard X:NAME =pressActivati​​ng_BeginStoryboard故事板={StaticResource的pressActivati​​ng}/>
                        < /Trigger.EnterActions>
                        < Trigger.ExitActions>
                            < BeginStoryboard X:NAME =pressedDeactivati​​ng_BeginStoryboard故事板={StaticResource的pressedDeactivati​​ng}/>
                        < /Trigger.ExitActions>
                    < /触发>
                    <触发属性=IsEnabledVALUE =FALSE>
                        < Trigger.EnterActions>
                            < BeginStoryboard故事板={StaticResource的DisableActivati​​ng}X:NAME =DisableActivati​​ng_BeginStoryboard/>
                        < /Trigger.EnterActions>
                    < /触发>
                < /ControlTemplate.Triggers>
            < /控件模板>
        < /Setter.Value>
    < /二传手>
< /样式和GT;


解决方案

你是如何的属性设置为False /真的吗?如果我复制你的code作为-IS,它完美的作品。必须有别的事情,你可能不希望影响它,就像按钮或正在清除绑定的东西动画。是否有更多的code,你可以发布,可以帮助澄清什么可以做呢?

下面是我测试以及在code:

 <窗​​口x:类=WpfApplication6.Window1
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1
HEIGHT =300
WIDTH =300>
<网格和GT;
< StackPanel的X:名称=QuickListButtonsStackPanel>
<按钮IsEnabled ={结合IsGenericUser}
CONTENT =绑定按钮/>
<按钮内容=更改绑定
点击=Button_Click/>
< / StackPanel的>
< /网格和GT;

 公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent();
QuickListButtonsStackPanel.DataContext =这一点;
}
公共静态只读的DependencyProperty IsGenericUserProperty =
DependencyProperty.Register(
IsGenericUser
typeof运算(布尔)
typeof运算(窗口1));公共BOOL IsGenericUser
{
{返回(布尔)的GetValue(IsGenericUserProperty); }
集合{的SetValue(IsGenericUserProperty,值); }
}私人无效Button_Click(对象发件人,RoutedEventArgs E)
{
IsGenericUser = IsGenericUser!;
}
}

编辑:
您可以添加一个文本框,以及看它是否工作正常,

 <按钮X:名称=的UIButton
IsEnabled ={结合IsGenericUser}
风格={StaticResource的BlackButtonStyle}
CONTENT =绑定按钮/>
< TextBlock的文本={绑定的ElementName =的UIButton,路径= IsEnabled}/>

看起来问题只是与风格的故事板,如果添加,做它仍然显示IsEnabled是假的,当它不应该是什么?

I have a dependency property defined in my window as below:

public static readonly DependencyProperty IsGenericUserProperty = DependencyProperty.Register("IsGenericUser", typeof (bool), typeof (MainWindow));
public bool IsGenericUser
{
    get { return (bool) GetValue(IsGenericUserProperty); }
    set { SetValue(IsGenericUserProperty, value); }
}

On my window's constructor I set the data context of the container holding the button:

QuickListButtonsStackPanel.DataContext = this;

I am binding the dependency property to the IsEnabled property of a button:

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

At startup IsGenericUser is true, so the button is enabled. When I set IsGenericUser to false, the button gets disabled. However, if I make IsGenericUser true again, nothing happens to the button and it remains disabled. What am I doing wrong?

Thanks!

edit: Here is the style I am using with the button. This style is causing the issue (if the button has no custom style it works fine):

<Style x:Key="BlackButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="MouseOverActivating">
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                            <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2F2F2F"/>
                            <SplineColorKeyFrame KeyTime="00:00:00.1270000" Value="#FF2391FF"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="MouseOverDeactivating">
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
                            <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2391FF"/>
                            <SplineColorKeyFrame KeyTime="00:00:00.2200000" Value="#FF2F2F2F"/>

                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="PressActivating">
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                            <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF2391FF"/>
                            <SplineColorKeyFrame KeyTime="00:00:00.1370000" Value="#FF48D6FF"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="PressedDeactivating" FillBehavior="Stop" >
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle">
                            <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF48D6FF"/>
                            <SplineColorKeyFrame KeyTime="00:00:00.2370000" Value="#FF2391FF"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="DisableActivating">
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                            <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFA7A7A7"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid>
                    <Rectangle Stroke="Transparent" RadiusX="5" RadiusY="5" x:Name="rectangle">
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF000000" Offset="0"/>
                                <GradientStop Color="#FF2F2F2F" Offset="1"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" OpacityMask="{x:Null}"/>
                    <Rectangle Stroke="Transparent" RadiusX="5" RadiusY="5" x:Name="WhiteGlow">
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#5BFFFFFF" Offset="0"/>
                                <GradientStop Color="#00FFFFFF" Offset="0.5"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsCancel" Value="False"/>
                    <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                    <Trigger Property="IsFocused" Value="True">
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource MouseOverActivating}" x:Name="MouseOverActivating_BeginStoryboard2"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource MouseOverActivating}" x:Name="MouseOverActivating_BeginStoryboard1"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource MouseOverDeactivating}" x:Name="MouseOverDeactivating_BeginStoryboard"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource MouseOverActivating}" x:Name="MouseOverActivating_BeginStoryboard"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="PressActivating_BeginStoryboard" Storyboard="{StaticResource PressActivating}"/>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard x:Name="PressedDeactivating_BeginStoryboard" Storyboard="{StaticResource PressedDeactivating}"/>
                        </Trigger.ExitActions>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource DisableActivating}" x:Name="DisableActivating_BeginStoryboard"/>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

解决方案

How are you setting the property to False/True? If I copy your code in as-is, it works perfectly. There has to be something else going on that you may not expect to effect it, like an animation on the button or something that is clearing the binding. Is there more code that you can post that may help clarify what could be doing this?

Here's the code I tested as well:

<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>

public partial class Window1 : Window
{
	public Window1()
	{
		InitializeComponent();
		QuickListButtonsStackPanel.DataContext = this;
	}
	public static readonly DependencyProperty IsGenericUserProperty =
		DependencyProperty.Register(
			"IsGenericUser",
			typeof(bool),
			typeof(Window1));

	public bool IsGenericUser
	{
		get { return (bool)GetValue(IsGenericUserProperty); }
		set { SetValue(IsGenericUserProperty, value); }
	}

	private void Button_Click(object sender, RoutedEventArgs e)
	{
		IsGenericUser = !IsGenericUser;
	}
}

EDIT: You can add a text box as well to see if it is working,

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

It looks like the problem is just with the style's storyboards, if you add that, does it still show that IsEnabled is false when it shouldn't be?

这篇关于WPF - IsEnabled绑定到的DependencyProperty工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆