A
A
Alexey Pavlov2015-11-22 19:58:49
WPF
Alexey Pavlov, 2015-11-22 19:58:49

How to set binding to UserControl from viewmodel?

Strange behavior of binding to two different dependency properties on the same object. There is a Game view model that has three properties - Red, Blue and Turn. The viewmodel is set to the DataContext. There is also a MageControl usercontrol installed by the Mage viewmodel. I bind one of the Game properties to MageControl.DataContext - everything works fine, the studio tells you which property to set from the viewmodel (Red or Blue).

<Window ...>
    <Window.DataContext>
        <game:Game/>
    </Window.DataContext>
    <local:MageControl DataContext="{Binding Red}"/> <!-- всё отлично работает -->
</Window>

Now I'm trying to pass a link to the current viewmodel to the usercontrol, created a dependency property, and try to bind data from the viewmodel to it.
public partial class MageControl : UserControl
{
    public int Turn
    {
        get { return (int)GetValue(TurnProperty); }
        set { SetValue(TurnProperty, value); }
    }

    public static DependencyProperty TurnProperty = DependencyProperty.Register("Turn", typeof(int), typeof(MageControl));
}

Now I'm trying to bind data:
<Window ...>
    <Window.DataContext>
        <game:Game/>
    </Window.DataContext>
    <StackPanel>
        <local:MageControl DataContext="{Binding Red}"/> <!-- привязка к DataContext отлично работает -->
        <local:MageControl DataContext="{Binding Blue}" Turn="{Binding Turn}"/> <!-- привязка к Turn не работает! -->
    </StackPanel>
</Window>

When I try to bind to the Turn property, it does not offer options from the view model (I want to bind Turn), but offers options from the MageControl view model.
In general, the question is the following - why do different properties of one element offer a different set of properties for binding. The MageControl.DataContext property is offered a set of properties from the window viewmodel, and the MageControl.Turn property is offered a set of properties from the MageControl viewmodel.
If you set up a binding to the parent, then it offers what you need:
<local:MageControl DataContext="{Binding Red}"
     Turn="{Binding Turn, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type game:Game}}}" /> <!-- работает -->

But the binding does not work - does not work even once.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2015-11-22
@lexxpavlov

<Window ...>
    <Window.DataContext>
        <game:Game/>
    </Window.DataContext>
    <StackPanel>
        <local:MageControl DataContext="{Binding Red}"/> <!-- привязка к DataContext отлично работает -->
        <local:MageControl DataContext="{Binding Blue}" Turn="{Binding Turn}"/> <!-- привязка к Turn не работает! -->
    </StackPanel>
</Window>

An unsourced binding binds to the DataContext. We look closely at the bindings:
{Binding Red} - the DataContext has not yet been set, the DataContext's parent is Game. Therefore, there will be a binding to Game.Red.
{Binding Blue} - DataContext not yet set, DataContext parent is Game. Therefore, there will be a binding to Game.Blue.
{Binding Turn} - The DataContext will be set on load to Game.Blue. Therefore, there will be a binding to Game.Blue.Turn.

M
MonkAlex, 2015-11-22
@MonkAlex

The intelligence in the studio sometimes does not work at all, sometimes it lies, sometimes it slows everything down wildly.
Therefore, I advise you to try to fill in the data according to logic, as it is written at the very top, to check whether it works like this. Snoop
helps a lot in debugging , plus the studio was able to sort of write about binding errors in Output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question