更新
下载小型测试应用here
我有一个自定义控件,包含边框,按钮和文本块。按下控件时,我会显示一个弹出选择器控件。我在使用视觉状态来正确启用和禁用控件时遇到了问题。
当我通常使用Normal和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>
更新
从控件中添加了显示可视状态更改触发器的代码。
private bool _isReadOnly;
public bool IsReadOnly
{
get { return _isReadOnly; }
set
{
_isReadOnly = value;
UpdateVisualState();
}
}
private void UpdateVisualState()
{
VisualStateManager.GoToState(this, IsReadOnly ? "Disabled" : "Normal", false);
}
答案 0 :(得分:2)
<击> 根据您的描述,您将IsReadOnly设置为true,但这不会使控件进入Disabled状态。您应该将IsEnabled设置为False。你能试一试吗?
想知道这是否有用......在PickerBoxButton的构造函数中,执行
this.IsEnabledChanged += (s, e) =>
{
if ((bool)e.NewValue)
VisualStateManager.GoToState((PickerBoxButton)s, "Disabled", true);
};
并且无论您在何处设置IsReadOnly属性,请将其替换为
this.YourCustomControl.IsEnabled = false;
击> <击> 撞击>
使用您的示例项目,我发现在设置矩形而不是按钮和文本块时,它实际上按预期工作。然后我怀疑这可能是因为GoToState触发了底层控件的可视状态,因为它们共享相同的可视状态名称。然后我将您的视觉状态名称从“正常”更改为“正常状态”,“禁用”更改为“已禁用”,然后一切正常。 :)
另外请删除我提供给您的代码,并使用原始的IsReadOnly属性来触发状态更改。