第一次从DependencyPropertyChanged处理程序调用时,视觉状态不会改变。
通过点击按钮或其他事件触发相同的视觉状态...
依赖属性
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { this.SetValue(IsSelectedProperty, value); }
}
// Using a DependencyProperty as the backing store for IsSelected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), typeof(NumericTextBlock), new PropertyMetadata(null, OnIsSelectedChanged));
/// <summary>
/// change event handler, fires when IsSelected property changes
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NumericTextBlock textBlock = d as NumericTextBlock;
if (d != null)
{
bool isSelected = (bool)(e.NewValue ?? false);
if (isSelected)
{
VisualStateManager.GoToState(textBlock, "Selected", true);
}
else
{
if (string.IsNullOrWhiteSpace( textBlock.valueTextBlock.Text))
{
VisualStateManager.GoToState(textBlock, "Normal", true);
}
else
{
VisualStateManager.GoToState(textBlock, "Edit", true);
}
}
}
}
为控件设置IsSelected
<custom:NumericTextBlock IsSelected="True"></custom:NumericTextBlock>
答案 0 :(得分:0)
显然,只需要这样做:
protected override void OnApplyTemplate()
{
...
base.OnApplyTemplate();
if (IsSelected)
{
VisualStateManager.GoToState(this, "Selected", true);
}
}