Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取附加属性中接收到的DependeyObject的模板';s回调_C#_.net_Wpf - Fatal编程技术网

C# 获取附加属性中接收到的DependeyObject的模板';s回调

C# 获取附加属性中接收到的DependeyObject的模板';s回调,c#,.net,wpf,C#,.net,Wpf,我正在WPF(.NET4)中开发一个应用程序 我创建了一个附加属性,以便轻松确定控件是否处于“有效”状态 如果实现VisualStates,则可以忽略动画的名称,只需更改状态即可。检查。你能提供你的控件的XAML吗?@Clemens:当然,我刚刚做了:)。但是我没有看到资源字典。非常感谢,我会阅读更多关于它的内容,看看它是否能帮助我。完全同意,可视状态将是一个更好的方法。也许最好发布一个新问题?我想你至少需要两个相互排斥的状态,例如,有效和无效,第一个有效和默认有效。请验证使用VisualSta

我正在WPF(.NET4)中开发一个应用程序

我创建了一个附加属性,以便轻松确定控件是否处于“有效”状态


如果实现VisualStates,则可以忽略动画的名称,只需更改状态即可。检查。

你能提供你的控件的XAML吗?@Clemens:当然,我刚刚做了:)。但是我没有看到资源字典。非常感谢,我会阅读更多关于它的内容,看看它是否能帮助我。完全同意,可视状态将是一个更好的方法。也许最好发布一个新问题?我想你至少需要两个相互排斥的状态,例如,有效和无效,第一个有效和默认有效。请验证使用VisualStates时所需的一些内容:您必须定义至少2个VisualStates(这在某种程度上是有意义的),并确保将控件配置为以默认状态显示作为开始。在XAML中定义的第一个状态将是默认状态,它必须与控件的默认表示形式相对应。
    /// <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; }

    (...)
    /// <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;
        }
    }
<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"/>
        <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);