I m 拟定一份WPF申请(NET 4)。
我拥有所附财产,是为了便于确定控制是否处于“Valid”状态。
/// <summary>
/// Initializes static members of the <see cref="ValidationProperty"/> class.
/// </summary>
static ValidationProperty()
{
// Register attached dependency property.
IsValidProperty = DependencyProperty.RegisterAttached("IsValid", typeof(bool),
typeof(ValidationProperty),
new FrameworkPropertyMetadata(true, ValidationValueChanged));
}
(...)
/// <summary>
/// Gets or sets the Dependency Property used to determine if an element is valid.
/// </summary>
public static DependencyProperty IsValidProperty { get; set; }
(...)
只要价值不实,我就想 play。 估算要么在控制本身范围内,要么在模板中加以界定。 故事板的名称永远不变,即“InvalidInput”。
我在此呼吁:
/// <summary>
/// Event raised when the "Is Valid" dependency property s value changes.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">The arguments passed to the event.</param>
private static void ValidationValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var invalidControl = sender as UserControl;
if (invalidControl == null || !(e.NewValue is bool))
{
return;
}
var isValid = (bool)e.NewValue;
if (isValid)
{
return;
}
var userControlInvalidInputStoryboard = invalidControl.Resources["InvalidInput"] as Storyboard;
var templatedInvalidInputStoryboard = invalidControl.Template.Resources["InvalidInput"] as Storyboard;
if (userControlInvalidInputStoryboard != null)
{
userControlInvalidInputStoryboard.Begin(invalidControl);
return;
}
if (templatedInvalidInputStoryboard != null)
{
templatedInvalidInputStoryboard.Begin(invalidControl, invalidControl.Template);
return;
}
}
现在的问题是,我无法找到控制或其模板中界定的故事板。
我怀疑这一点,是因为依赖物被投给使用者控制而不是真正的类型。
是否有办法检索模板及其故事板?
Edit:根据要求,这里是使用这一附设财产控制权的XAML。
<UserControl
(...)
xmlns:AttachedProperties="clr-namespace:Controls.AttachedProperties"
x:Name="Root">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="TextBox"
Text="{Binding Path=(AttachedProperties:TextProperty.Text), UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource AncestorType={x:Type Custom:UsernameTextBox}}, Mode=TwoWay,
TargetNullValue={x:Static Custom:UsernameTextBox.DefaultKeyword}, FallbackValue=DefaultKeyword}"
HorizontalAlignment="Left" Width="292" Style="{DynamicResource StandardTextBoxStyle}"
Height="35" VerticalAlignment="Top" TabIndex="0"
MaxLines="1" MaxLength="30"
FontSize="16" GotFocus="TextBoxGotFocus" LostFocus="TextBoxLostFocus" TextChanged="TextBoxTextChanged" FontWeight="Bold"/>
</Grid>
在这里,我如何立即进行控制:
<Custom:UsernameTextBox x:Name="UsernameTextbox"
AttachedProperties:ValidationProperty.IsValid="{Binding CodexLoginService.UsernameIsValid, UpdateSourceTrigger=PropertyChanged}"
AttachedProperties:TextProperty.Text="{Binding CodexLoginService.Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
KeyUp="OnUsernameChanged"/>
第2号:按照提议,我在模板中设立了一个视觉国家小组:
<VisualStateGroup x:Name="InvalidStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.2" To="Invalid"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Invalid">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="InvalidFrame">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/> </DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
试图改变控制状态并不奏效,这种方法使:
VisualStateManager.GoToState(invalidControl, "Invalid", true);