Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to MVVM and having a problem, I have a user control (view) that I would like to use in other user controls.

TextBoxView.xaml
<UserControl x:Class="SwTest.View.TextBoxView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vm="clr-namespace:SwTest.ViewModel"
             mc:Ignorable="d" >

    <UserControl.DataContext>
        <vm:TextBoxViewModel />
    </UserControl.DataContext>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="110" />
            <ColumnDefinition Width="350" />
        </Grid.ColumnDefinitions>

        <TextBox Grid.Column="0" Text="{Binding TxtLabel}" />
        <TextBox Grid.Column="1" Text="{Binding EdtText}" />
    </Grid>
</UserControl>


TestScreenView.xaml
<UserControl x:Class="SwTest.View.TestScreenView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:v="clr-namespace:SwTest.View"
             xmlns:vm="clr-namespace:SwTest.ViewModel"
             mc:Ignorable="d" >

    <UserControl.DataContext>
        <vm:TestScreenViewModel />
    </UserControl.DataContext>

    <Grid>
        <v:TextBoxView TxtLabel="Label 1" />
        <v:TextBoxView TxtLabel="Label 2" />
    </Grid>
</UserControl>

The property TxtLabel is not found, I am using MVVM light. I have tyied using dependency property. I know I can make it work by puting the code in the codebehind file. Is there a correct way to do this in MVVM.

Thanks.
Posted
Updated 14-Nov-12 4:20am
v2
Comments
Jorge Montoya 14-Nov-12 8:57am    
The DataContext is
<usercontrol.datacontext>
<vm:textboxviewmodel>


the compiler still gives an error
"The Member "TextLabel" is not recognized or is not accesible"
Shmuel Zang 14-Nov-12 9:05am    
It can be helpful if you post also the code of your TextBoxView control. Use "Improve question" above.

1 solution

What's the DataContext of your TextBoxView? It seems like you want to bind to the properties of the TextBoxView itself. So, you can set the DataContext of the TextBoxView to itself. Something like:


XML
<Grid>
    <v:TextBoxView DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
                    TxtLabel="Label 1" />
    <v:TextBoxView DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
                    TxtLabel="Label 2" />
</Grid>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900