Переменная DataBind к свойству ListBox.SelectedItem - PullRequest
0 голосов
/ 11 августа 2011

У меня есть ListBox (lstBxsources), который заполнен правильно и работает так, как и должен.

<ListBox Name="lstBxSources" ItemsSource="{Binding}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" ToolTipService.ToolTip="{Binding Path=Description}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

У меня также есть UserControl (MyUserControl).

<MainControl:MyControl x:Name="MyUserControl" Grid.Row="1"/>

MyUserControl имеет зависимостьСвойство с именем 'CurrentSourceProperty'

    public SourceInfo CurrentSource
    {
        get { return (SourceInfo)GetValue(CurrentSourceProperty); }
        set { SetValue(CurrentSourceProperty, value); }
    }

    public static readonly DependencyProperty CurrentSourceProperty =
        DependencyProperty.Register("CurrentSource", typeof(SourceInfo), typeof(MyControl), new PropertyMetadata(null));

У меня есть привязка CurrentSource к SelectedItem объекта lstBxSources следующим образом:

 MyUserControl.SetBinding(MyControl.CurrentSourceProperty, new Binding() { Source = lstBxSources.SelectedItem});

Это работает изначально, но не будет обновляться, когда SelctedItem будетизменился.

Есть идеи, почему он не обновится для меня?

1 Ответ

1 голос
/ 11 августа 2011

Понял ... исправьте свою привязку следующим образом:

MyUserControl.SetBinding(MyControl.CurrentSourceProperty,
new Binding() { 
  Source = lstBxSources, 
  Path= new PropertyPath("SelectedItem")
  });

Если я правильно понял, поместите этот код в MyUserControl:

MyAnotherControl.SetBinding(AnotherControl.currentSourceInfoProperty,
    new Binding()
    {
        Source = this,
        Path = new PropertyPath("CurrentSource"),
            Mode = BindingMode.TwoWay
    });
...