2011-03-18 61 views
3

UPDATE:WP7可視狀態問題

下載一個小的測試應用程序here

我有包括邊框,按鈕和文本塊的自定義控制。當控制按下時,我顯示一個彈出式選取控件。我在使用可視狀態來正確啓用和禁用控件時遇到問題。

當我啓用和正常使用正常和殘疾人可視狀態正常工作禁用該控件:

啓用:

Enabled

禁用:

Disabled

如果我點擊該控件,然後禁用背景保持白色的控件:

disabled

任何想法?

更新:我認爲我錯過了我的視覺狀態,由系統設置的屬性。我希望有人能夠確定它是什麼,所以我可以覆蓋它。

下面是式:

<Style 
    TargetType="Controls:PickerBoxButton"> 

    <Setter 
     Property="Background" 
     Value="Transparent" /> 

    <Setter 
     Property="BorderBrush" 
     Value="{StaticResource PhoneForegroundBrush}" /> 

    <Setter 
     Property="Foreground" 
     Value="{StaticResource PhoneForegroundBrush}" /> 

    <Setter 
     Property="BorderThickness" 
     Value="{StaticResource PhoneBorderThickness}" /> 

    <Setter 
     Property="FontFamily" 
     Value="{StaticResource PhoneFontFamilyNormal}" /> 

    <Setter 
     Property="FontSize" 
     Value="{StaticResource PhoneFontSizeMediumLarge}" /> 

    <Setter 
     Property="Padding" 
     Value="8,3,8,5" /> 

    <Setter 
     Property="Template"> 

     <Setter.Value> 

      <ControlTemplate 
       TargetType="Controls:PickerBoxButton"> 

       <Grid 
        Background="Transparent"> 

        <VisualStateManager.VisualStateGroups> 

         <VisualStateGroup 
          x:Name="CommonStates"> 

          <VisualState 
           x:Name="Normal"> 
           <Storyboard> 

            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource PhoneForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 

            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="PickerButton" 
             Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource PhoneTextBoxBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 

            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="PickerText" 
             Storyboard.TargetProperty="Foreground"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource PhoneTextBoxForegroundBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 

           </Storyboard> 

          </VisualState> 

          <VisualState 
           x:Name="Disabled"> 
           <Storyboard> 

            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="BorderBrush"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource PhoneDisabledBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 

            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="PickerButton" 
             Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource PhoneChromeBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 

            <ObjectAnimationUsingKeyFrames 
             Storyboard.TargetName="PickerText" 
             Storyboard.TargetProperty="Foreground"> 
             <DiscreteObjectKeyFrame 
              KeyTime="0" 
              Value="{StaticResource PhoneDisabledBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 

           </Storyboard> 
          </VisualState> 

         </VisualStateGroup> 

        </VisualStateManager.VisualStateGroups> 

        <Border 
         x:Name="ButtonBackground" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         CornerRadius="0" 
         Background="{TemplateBinding Background}" 
         Margin="6,8,8,0" 
         > 

         <Button 
          x:Name="PickerButton" 
          BorderThickness="0" 
          Height="64" 
          HorizontalAlignment="Left" 
          Margin="-12,-12,0,-12" 
          VerticalAlignment="Top" 
          Width="700"> 

          <StackPanel 
           Orientation="Horizontal" 
           Width="700"> 
           <TextBlock 
            x:Name="PickerText" 
            Margin="-2, 0, 0, 0" 
            /> 
          </StackPanel> 

         </Button> 

        </Border> 

       </Grid> 

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

UPDATE

從表示視覺狀態變化觸發的控制添加的代碼。

private bool _isReadOnly; 
public bool IsReadOnly 
{ 
    get { return _isReadOnly; } 
    set 
    { 
     _isReadOnly = value; 

     UpdateVisualState(); 
    } 
} 

private void UpdateVisualState() 
{ 
    VisualStateManager.GoToState(this, IsReadOnly ? "Disabled" : "Normal", false); 
} 
+0

嗨你是什麼意思'點擊控件,然後禁用控件'?你能告訴我禁用控件的代碼嗎?謝謝。 – 2011-04-29 07:15:57

+0

意思是觸摸控件,使其具有焦點,然後將該控件的IsReadOnly屬性設置爲true,從而調用VisualStateManager.GotoState傳遞Disabled。請參閱上面的代碼和Style中的Visual狀態。當控件沒有焦點時,更改爲「已禁用」狀態將起作用,但當控件具有焦點時,文本將變爲白色。必須是我沒有在視覺狀態下設置的屬性,但無法找到哪個屬性。 – 2011-04-29 11:06:32

+0

是否可以這樣做,因爲當控件獲得焦點時,下劃線按鈕處於按下狀態,按鈕本身不知道它應該被禁用,因爲您沒有將其設置爲? – 2011-04-29 13:08:41

回答

2

從你的描述,你IsReadOnly設置爲true,但是這不會使控制轉到Disable狀態。您應該將IsEnabled設置爲False。你可以試試看嗎?

不知道這會工作......在你PickerBoxButton的構造,做

this.IsEnabledChanged += (s, e) => 
    { 
     if ((bool)e.NewValue) 
      VisualStateManager.GoToState((PickerBoxButton)s, "Disabled", true); 
    }; 

和無論你設置你的IsReadOnly屬性,與

this.YourCustomControl.IsEnabled = false; 


更換

隨着您的示例項目,我發現動畫矩形時按鈕和文本塊實際上按預期工作。然後我懷疑這可能是因爲GoToState觸發底層控件的視覺狀態,因爲它們共享相同的視覺狀態名稱。然後我將你的視覺狀態名稱從'Normal'改爲'NormalState','Disabled'爲'Disabled',然後一切正常。 :)

也請刪除我給你的代碼,並使用您的原始IsReadOnly屬性觸發狀態更改。

+0

已經嘗試過。無法直接在控件上設置IsEnabled,因爲這是實現一個接口的多個控件之一,而IsReadOnly是一個接口menber,所以在IsReadOnly setter中設置IsEnabled,然後在可視狀態更改中檢查IsEnabled。好消息是禁用時背景會變暗。壞消息是背景保持黑暗時再次啓用。很確定有一個背景屬性需要在IsEnabled設置的可視狀態中進行設置。我需要找到那個財產。 – 2011-04-30 23:30:45

+0

對不起,我不是100%確定你爲什麼不能在自定義控件中設置IsEnabled屬性。它是從Control繼承的嗎?你介意給我看你的自定義控件的完整代碼嗎?謝謝 – 2011-04-30 23:42:17

+0

我可以設置IsEnabled如果我想,但我寧願間接設置它。兩種方式的結果都一樣現在的主要問題是當IsEnabled設置爲true時,背景沒有恢復。我們將制定一個測試解決方案來展示問題。即將發佈位置。 – 2011-05-01 01:33:44