更新:
下载一个小型测试应用程序 here
我有一个由边框、按钮和文本 block 组成的自定义控件。当按下该控件时,我会显示一个弹出选择器控件。我在使用视觉状态正确启用和禁用控件时遇到问题。
当我使用“正常”和“禁用”视觉状态正常启用和禁用该控件时,它工作正常:
启用:
禁用:
如果我单击该控件然后禁用该控件,背景将保持白色:
有什么想法吗?
更新:我认为我的视觉状态中缺少一个由系统设置的属性。我希望有人能够识别它是什么,以便我可以覆盖它。
这是样式:
<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>
更新
从控件中添加了显示视觉状态更改触发器的代码。
private bool _isReadOnly;
public bool IsReadOnly
{
get { return _isReadOnly; }
set
{
_isReadOnly = value;
UpdateVisualState();
}
}
private void UpdateVisualState()
{
VisualStateManager.GoToState(this, IsReadOnly ? "Disabled" : "Normal", false);
}
最佳答案
<罢工> 根据您的描述,您将 IsReadOnly 设置为 true,但这不会使控件进入禁用状态。您应该将 IsEnabled 设置为 False。你能尝试一下吗?
想知道这是否可行...在 PickerBoxButton 的构造函数中,执行以下操作
this.IsEnabledChanged += (s, e) =>
{
if ((bool)e.NewValue)
VisualStateManager.GoToState((PickerBoxButton)s, "Disabled", true);
};
无论您在何处设置 IsReadOnly 属性,请将其替换为
this.YourCustomControl.IsEnabled = false;
罢工><罢工>罢工>
<小时/>通过您的示例项目,我发现当对矩形而不是按钮和文本 block 进行动画处理时,它实际上按预期工作。然后我怀疑这可能是因为 GoToState 触发底层控件的视觉状态发生变化,因为它们共享相同的视觉状态名称。然后,我将您的视觉状态名称从“正常”更改为“正常状态”,将“禁用”更改为“禁用”,然后一切正常。 :)
另外,请删除我给您的代码,并使用您原来的 IsReadOnly 属性来触发状态更改。
关于silverlight - WP7 视觉状态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5358521/