none
DependencyProperty of type XmlElement RRS feed

  • Question

  •  

    Hi,

    Is is possible to have a DependencyProperty of type XmlElement? I have created a UserControl and need to have a property of type XmlElement which will be data bound in some xaml. I've copied below the relevant XAML and code from my user control. Basically OnDataChanged is never called. If I change the type of the DependencyProperty to string, it does get called and I receive the inner text for the Book element. I don't know what I'm missing to get it working as an XmlElement.

     

    Any ideas?

     

    Thanks

    Pat.

     

    Xaml :

    <XmlDataProvider x:Key="myData" XPath="Inventory/Books">

    <x:XData>

    <Inventory xmlns="">

    <Books>

    <Book ISBN="0-7356-0562-9" Stock="in" Number="9">

    <Title>XML in Action</Title>

    <Summary>XML Web Technology</Summary>

    </Book>

    </Books>

    </Inventory>

    </x:XData>

    </XmlDataProvider>

     

    <cust:MyControl Data="{Binding Source={StaticResource myData}, XPath=Book}"></cust:MyControl>

     

    Snippet from User Control Code :

     

    public static readonly DependencyProperty DataProperty =

    DependencyProperty.Register("Data",typeof(XmlElement),typeof(MyControl),

    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnDataChanged)));

    public XmlElement Data

    {

    get

    {

    return (XmlElement)GetValue(DataProperty);

    }

    set

    {

    SetValue(DataProperty, value);

    }

    }

    private static void OnDataChanged(DependencyObject obj,

    DependencyPropertyChangedEventArgs args)

    {

    // Do some processing with the XmlElement here......

    MyControl myCtrl = (MyControlt)obj;

    }

    Thursday, March 6, 2008 7:02 PM

Answers

  • Got it to work in the end by changing the DependencyProperty type to Object. Then in the callback, just cast it to XmlElement.

    Thursday, March 6, 2008 11:09 PM

All replies

  • Maybe you'd try to mark your property with the Bindable attribute?

     

    Thursday, March 6, 2008 7:25 PM
  • Could you tell me how exactly to do that? Is it something in FrameworkPropertyMetadataOptions?

     

    Thanks.

     

    Thursday, March 6, 2008 8:12 PM
  • Got it to work in the end by changing the DependencyProperty type to Object. Then in the callback, just cast it to XmlElement.

    Thursday, March 6, 2008 11:09 PM