Pergunta

I have a little problem here. I've created custom TreeView using RadTreeView. It all works nice, but I've encountered an obstacle. I've set DependencyProperty for SelectedItem in TreeView. I nest my control in View, bind property to SelectedItem in TwoWay mode, but bound property won't update, it's null all the time, despite DependencyProperty value being set.

Here's tree xaml:

    <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
          xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
          xmlns:sdk='http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk' 
          xmlns:telerik='http://schemas.telerik.com/2008/xaml/presentation' x:Name='this' >     
       <Grid.Resources>
          <DataTemplate x:Key='ChildTemplate'>
               <TextBlock Text='{Binding Path=ChildPath}' Margin='5,0' />
          </DataTemplate>
          <telerik:HierarchicalDataTemplate x:Key='NameTemplate' ItemsSource='{Binding ChildrenCollectionPath}' ItemTemplate='{StaticResource ChildTemplate}'>
               <TextBlock Text='{Binding Path=ParentPath }' Padding='7'/>
          </telerik:HierarchicalDataTemplate>
       </Grid.Resources>
    <telerik:RadTreeView x:Name='rtvTreeView' Padding='5' BorderThickness='0' IsEditable='False' IsLineEnabled='True' IsExpandOnDblClickEnabled='False' ItemTemplate='{StaticResource NameTemplate}' />
    </Grid>

Below is way I nest the control in View:

<windows:TreeViewReuse CollectionSource="{Binding SitesCollectionWithAddress}" ParentPath="Napis" Grid.Column="0" BorderThickness="2" SelectedItemD="{Binding SelectedSide, ElementName=this, UpdateSourceTrigger=Explicit, Mode=TwoWay}"                                       ChildPath="FullAddress" ChildrenCollectionPath="AdresyStrony" BorderBrush="Red" DoubleClickCommand="{Binding TreeViewDoubleClick}">
</windows:TreeViewReuse>

And here's Tree's code behind in parts:

public partial class TreeViewReuse : UserControl
{
    static Telerik.Windows.FrameworkPropertyMetadata propertyMetaData = new Telerik.Windows.FrameworkPropertyMetadata(null,
        Telerik.Windows.FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(SelectedItemChangedCallback));
    public object SelectedItemD
    {
        get { return GetValue(SelectedItemDProperty); }
        set { SetValue(SelectedItemDProperty, value); }
    }
    public static readonly DependencyProperty SelectedItemDProperty =
        DependencyProperty.Register("SelectedItemD", typeof(object), typeof(TreeViewReuse), propertyMetaData);

    public TreeViewReuse()
    {
        InitializeComponent();

        Loaded += new RoutedEventHandler(TreeViewReuse_Loaded);
    }

    void treeView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
    {
        SelectedItemD = _treeView.SelectedItem;
    }

    static private void SelectedItemChangedCallback(DependencyObject dp, DependencyPropertyChangedEventArgs e)
    {
    }

Does anyone have an idea why property bound to SelectedItemD does not update? I don't care about setting tree's selected item from it, I only want to set it to selected item.

Here's property:

public StronaSprawy SelectedSide
{
     get
     {
         return _selectedSide;
     }
     set
     {
         _selectedSide = value;
     }
}
Foi útil?

Solução

Your Dependency Property looks fine.. all except for that Telerik.Windows.FrameworkPropertyMetadata instance.

Silverlight does not support setting meta data options, so I cant think how the Telerik implementation will achieve that. It is possible that Telerik have their own DP implementation, or even that this type of property meta data only works with their controls.

Try using the standard System.Windows.PropertyMetaData type instead and see if that works for you.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top