English 中文(简体)
图一 随附财产回击中收到的附庸物品模板
原标题:Fetch the template of a DependyObject received in an Attached Property s callback
  • 时间:2012-01-12 19:07:26
  •  标签:
  • c#
  • .net
  • wpf

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);
最佳回答

如果你执行视觉国家,你就能够忘记悲观和公正改变国家的名称。 ,这一条款。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签