I am creating user control that will act as container for other control. I found code on StackOverflow and I created user control using that sample. Here is code of my user control.
<UserControl x:Class="ClassLibrary1.UserControl1"
xmlns:local="clr-namespace:ClassLibrary1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Template>
<ControlTemplate>
<StackPanel>
<Button Content="Custom Control Text Area 1" IsEnabled="{Binding IsEnabled}" />
<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>
<TextBlock Text="Custom Control Text Area 2" />
</StackPanel>
</ControlTemplate>
</UserControl.Template>
Code behind is as below
namespace ClassLibrary1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
}
}
}
Then I added this user control in other windows application project and added button in this user control so code looks like below
<StackPanel>
<l:UserControl1>
<StackPanel >
<Button Click="Button_Click" Content="Click"/>
<Button Content="My Control's Content" IsEnabled="{Binding Isbuttonenable}"/>
</StackPanel>
</l:UserControl1>
</StackPanel>
Now from my windows application project is working fine and Button control is able to Enable/Disable based on Isbuttonenable valye.
Now I updated User control and added ViewModel to user control. Here is code behind code of user control
namespace ClassLibrary1
{
public partial class UserControl1 : UserControl
{
UserControl1ViewModel model = new UserControl1ViewModel();
public UserControl1()
{
InitializeComponent();
this.DataContext = model;
}
}
}
Now when I use this updated control in my Windows Application project, data-biding is no longer working. Button is now no longer able to Enble/Disable based on Isbuttonenable value. What may be the solution for this. I want to keep ViewModel in my user control.
I tried lot of way and goggle lot but not able get the solution for this.