简体   繁体   中英

How can i get the value of the custom property?

I was looking for a way to add a custom property to a xaml control. I found this solution: Adding custom attributes to an element in XAML?

Class1.cs:

public static Class1
{
    public static readonly DependencyProperty IsTestProperty = 
       DependencyProperty.RegisterAttached("IsTest",
                                          typeof(bool), 
                                          typeof(Class1),
                                          new FrameworkPropertyMetadata(false));

    public static bool GetIsTestProperty(UIElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        return (bool)element.GetValue(IsTestProperty);
    }

    public static void SetIsTestProperty(UIElement element, bool value)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        element.SetValue(IsTestProperty, value);
    }
}

UserControl.xaml

<StackPanel x:Name="Container">
    <ComboBox x:Name="cfg_Test" local:Class1.IsTest="True" />
    <ComboBox x:Name="cfg_Test" local:Class1.IsTest="False" />
    ...
...

Now is my question, how can i get the value of the property?

Now I want to read the value of all elements, in the StackPanel.

// get all elementes in the stackpanel
foreach (FrameworkElement child in 
            Helpers.FindVisualChildren<FrameworkElement>(control, true))
{
    if(child.GetValue(Class1.IsTest))
    {
        //
    }
}

but child.GetValue(Class1.IsTest) is always false... what's wrong?

First of all, it seems, that your code is full of errors, so iam not sure, if you did not copy it properly, or whats the reason for that.

So whats wrong in your example?

  • The getter and setter of your DependencyProperty are wrongly created. (There should be no "Property" attached to the names.) It chould be:
public static bool GetIsTest(UIElement element)
{
    if (element == null)
    {
        throw new ArgumentNullException("element");
    }

    return (bool)element.GetValue(IsTestProperty);
}

public static void SetIsTest(UIElement element, bool value)
{
    if (element == null)
    {
        throw new ArgumentNullException("element");
    }

    element.SetValue(IsTestProperty, value);
}
  • Secondly, both of your child controls of the StackPanel share the same name, thats not possible too.
  • And third, you getting the property in your foreach-statement wrongly. This should be:
if ((bool)child.GetValue(Class1.IsTestProperty))
{
  // ...
}
  • Be sure, that your Helpers.FindVisualChildren is working correctly. You could use the following instead:
foreach (FrameworkElement child in Container.Children)
{
   // ...
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM