UPDATE:WP7可視狀態問題
下載一個小的測試應用程序here
我有包括邊框,按鈕和文本塊的自定義控制。當控制按下時,我顯示一個彈出式選取控件。我在使用可視狀態來正確啓用和禁用控件時遇到問題。
當我啓用和正常使用正常和殘疾人可視狀態正常工作禁用該控件:
啓用:
禁用:
如果我點擊該控件,然後禁用背景保持白色的控件:
任何想法?
更新:我認爲我錯過了我的視覺狀態,由系統設置的屬性。我希望有人能夠確定它是什麼,所以我可以覆蓋它。
下面是式:
<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);
}
嗨你是什麼意思'點擊控件,然後禁用控件'?你能告訴我禁用控件的代碼嗎?謝謝。 – 2011-04-29 07:15:57
意思是觸摸控件,使其具有焦點,然後將該控件的IsReadOnly屬性設置爲true,從而調用VisualStateManager.GotoState傳遞Disabled。請參閱上面的代碼和Style中的Visual狀態。當控件沒有焦點時,更改爲「已禁用」狀態將起作用,但當控件具有焦點時,文本將變爲白色。必須是我沒有在視覺狀態下設置的屬性,但無法找到哪個屬性。 – 2011-04-29 11:06:32
是否可以這樣做,因爲當控件獲得焦點時,下劃線按鈕處於按下狀態,按鈕本身不知道它應該被禁用,因爲您沒有將其設置爲? – 2011-04-29 13:08:41