2009-06-08 277 views
3

我已經在我的窗口,如下定義的依賴項屬性:WPF - 的IsEnabled綁定到的DependencyProperty不能正常工作

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; 

我的依賴屬性綁定到一個按鈕的IsEnabled屬性:

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

啓動時IsGenericUser爲true,因此該按鈕已啓用。當我將IsGenericUser設置爲false時,該按鈕被禁用。但是,如果我使IsGenericUser再次爲true,則按鈕沒有任何反應,並且它保持禁用狀態。 我在做什麼錯?

謝謝!

編輯: 這是我用這個按鈕的樣式。這種風格導致了問題(如果該按鈕沒有自定義樣式,它工作正常):

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

對不起,在使用我上面描述的所有東西的測試項目中,所有工作都很好。其他的東西,特定於我的項目,可能是錯誤的。 – 2009-06-08 18:44:49

+1

還有其他的那個目標是那個按鈕嗎?你是否通過代碼設置了任何屬性或將任何動畫應用於它? – rmoore 2009-06-08 18:49:59

+0

是的,我將動畫應用於按鈕的樣式。這可能是導致問題的原因。如果我不使用按鈕樣式,它一切正常。 – 2009-06-08 19:22:39

回答

5

你是如何的屬性設置爲False /真的嗎?如果我按照原樣複製代碼,那麼它可以很好地工作。必須有其他的事情發生,你可能不希望影響它,比如按鈕上的動畫或者正在清除綁定的東西。是否有更多的代碼可以發佈,可能有助於澄清可能會做什麼?

下面是我測試以及代碼:

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

編輯: 您可以添加一個文本框,以及看它是否是工作,

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

看起來問題就在於風格的故事板,如果添加它,它是否仍然顯示當它不應該是IsEnabled是錯誤的?

0

嘗試

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

Path=不應該是必需的,但它可能使一個differerce。

並且通過在數據上下文中使用this,你確定這是正確的嗎?這不會使控制本身成爲上下文。我還沒有看到你的其他代碼,但這看起來不對。

0

1)創建了一個名爲DisableDeactivating新故事板並設置FillBehavior = 「停止」(Nicholas的建議) 2)然後,在的IsEnabled =假觸發的Trigger.ExitActions加入BeginStoryboard爲DisableDeactivating。