Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF MVVM
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 14-Nov-12 2:28am
Edited 14-Nov-12 4:20am
v2
Comments
Jorge Montoya at 14-Nov-12 8:57am
   
The DataContext is



 
the compiler still gives an error
"The Member "TextLabel" is not recognized or is not accesible"
Shmuel Zang at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 330
1 BillWoodruff 314
2 George Jonsson 274
3 CPallini 265
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100