Providing multiple data binding sources

I feel like I am missing the fundamental concept of WPF when it comes to data binding, but I cannot find the right Google keyword combination to determine what I need, so maybe the SO community can help. :)

I have a WPF user control that needs to bind data to two separate objects for proper display. Both objects must be dynamically configured from an external source. So far, I just used the form's DataContext property to bind dynamic objects, but this only allows you to reference one object. I feel this is a simple problem, and I must be missing something obvious.

My previous attempt looks something like this:

<UserControl.Resources>
    <src:Person x:Key="personSource" />
    <src:Job x:Key="jobSource" />
</UserControl.Resources>
<TextBox Text="{Binding Source={StaticResource personSource}, Path=Name" />
<TextBox Text="{Binding Source={StaticResource jobSource}, Path=Address" />

This will be associated with any default values. I give classes just fine, but if I try to dynamically set objects into code (as shown below), I don't see any changes.

Person personSource = FindResource("personSource") as Person;
personSource = externalPerson;
Job jobSource= FindResource("jobSource") as Job;
jobSource = externalJob;

What am I missing?

+3
source share
2 answers

I would probably use CustomControl with two DependencyProperties. Then the external site that uses your custom control can associate the data that they want with this control, and with the help of a custom control, you can customize how you control it in different situations.

The user control code looks something like this:

public class CustomControl : Control
{
    public static readonly DependencyProperty PersonProperty =
        DependencyProperty.Register("Person", typeof(Person), typeof(CustomControl), new UIPropertyMetadata(null));
    public Person Person
    {
        get { return (Person) GetValue(PersonProperty); }
        set { SetValue(PersonProperty, value); }
    }


    public static readonly DependencyProperty JobProperty =
        DependencyProperty.Register("Job", typeof(Job), typeof(CustomControl), new UIPropertyMetadata(null));
    public Job Job
    {
        get { return (Job) GetValue(JobProperty); }
        set { SetValue(JobProperty, value); }
    }

    static CustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
    }
}

Generic.xaml is a file that should be created for you, and may have a style that looks something like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication3">


    <Style TargetType="{x:Type local:CustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel>
                            <TextBox Text="{TemplateBinding Person.Name}" />
                            <TextBox Text="{TemplateBinding Job.Address}" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

, , - .

<src:CustomControl Person="{Binding Person}" Job="{Binding Job}" />
+4

, , , StaticResource. , . Binding MarkupExtension DependencyObject, DynamicResource.

depedency , Person Job.

DataContext UserControl .

DataContext="{Binding RelativeSource={RelativeSource Self}}"

.

<TextBox Text="{Binding Path=Person.Name" />
<TextBox Text="{Binding Path=Job.Address" />

<TextBox Text="{Binding Source=Person, Path=Name" />
<TextBox Text="{Binding Source=Job, Path=Address" />
+1

Source: https://habr.com/ru/post/1699127/


All Articles