V
V
Vyacheslav Zolotov2014-07-24 12:45:37
WPF
Vyacheslav Zolotov, 2014-07-24 12:45:37

What is wrong with data binding?

Hello!
An error occurred while binding the data.
There are parent and child controls in which the SelectedIndex dependency property is implemented.
Next, in the parent control, I associate this property with the same property of the subordinate:

//this = currentObject; 
//typeof(this) == typeof(Reader);

var indexBinding = new Binding();
indexBinding.Mode = BindingMode.TwoWay;
//указываю подчненный контролл как источник данных
indexBinding.Source = epubConrol.SelectedIndex;
//Очищаю свойство зависимостей и создаю привязку 
currentObject.ClearValue(Reader.SelectedIndexProperty);
currentObject.SetBinding(Reader.SelectedIndexProperty, indexBinding);

Then, if you change the SelectedItem of the main controller using the page buttons, everything works fine, the controller scrolls the Item of the slave controller. If I swipe on a child control, then the change notification does not come to the parent control.
What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2014-07-24
@Sumor

For TwoWay, you must specify using Path or XPath.
Try like this:

//this = currentObject; 
//typeof(this) == typeof(Reader);

var indexBinding = new Binding();
indexBinding.Mode = BindingMode.TwoWay;
//указываю подчненный контролл как источник данных
indexBinding.ElementName = "epubConrol";
indexBinding.Path = new PropertyPath("SelectedIndex");
//Очищаю свойство зависимостей и создаю привязку 
currentObject.ClearValue(Reader.SelectedIndexProperty);
currentObject.SetBinding(Reader.SelectedIndexProperty, indexBinding);

In Xaml it would look something like this:
<ListBox x:Name="lst1" SelectedIndex="{Binding ElementName=lst2, Path=SelectedIndex, Mode=TwoWay}">
    <system:String>1 строка</system:String>
    <system:String>2 строка</system:String>
</ListBox>
<ListBox Grid.Column="1" x:Name="lst2">
    <system:String>1 строка</system:String>
    <system:String>2 строка</system:String>
</ListBox>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question