WPF user control binding (FAQ?)

Goran Bervar 21 Reputation points
2022-10-12T12:56:54.4+00:00

Hi,

I'm experienced programmer comming form C++ but fairly new to both C# and WPF. In my learning curve I'd like to create a custom XAML that will be my representation of class in XAML. Class has three simple object's properties, say string values 'Name', 'ID' and 'Connected' . Custom control will show all three properties in three-row grid and will be part of class library. It's purpose is to be used in main appliaction's XAML widnow(s) to show all three properties of different objects at different places. While testing in single application I can set DataContext to instance of my class in cs, eg

DataContext = new MyClass1();

and everythig works as expcted.

What I don't know how to do is how to bind DataContext to different objects in my main application, preferablly in MVVM, eg (pseudo code):

<control:MyCustomControl Grid.Row = "0" x:Name="customControl1" DataContext="{Binding Object1, Mode = "OneWay"}" />
<control:MyCustomControl Grid.Row = "7" x:Name="customControl2" DataContext="{Binding Object2, Mode = "OneWay"}" />

and

class ViewModel: BindableBase //prism
{
MyClass object1;
public property MyClass Object1{ get{returj object1;}}

MyClass object2;
public property MyClass Object2{ get{returj object2;}}
}

What is the easiest way to get it done? Am I on completly wrong track?

Best Regards
Goran

Developer technologies XAML
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-10-13T02:12:45.09+00:00

    Hi, @Goran Bervar . Welcome Microsoft Q&A.
    You are on the right track. You would usually have a MainViewModel like

     public class ViewModel  
        {  
            public ViewModel1 M1 { get; set; }  
            public ViewModel2 M2 { get; set; }  
             
        }  
        public class ViewModel1  
        {  
            public string Name1 { get; set; }  
        }  
        public class ViewModel2  
        {  
            public string Name2 { get; set; }  
        }  
    

    and would use it in your XAML like

     <Window.DataContext>  
            <local:ViewModel/>  
        </Window.DataContext>  
        <StackPanel>  
             
            <local:UserControl1 x:Name="uc1" DataContext="{Binding M1}"/>  
            <local:UserControl1 x:Name="uc2" DataContext="{Binding M2}"/>  
        </StackPanel>  
    

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.