Вы должны создать DependencyProperty в вашем пользовательском элементе управления, который будет использоваться для привязки флажка и описания.Что-то вроде этого
public partial class UserControl1 : UserControl
{
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
// Using a DependencyProperty as the backing store for IsChecked. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register("IsChecked", typeof(bool), typeof(UserControl1), new UIPropertyMetadata(false));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new UIPropertyMetadata(string.Empty));
public UserControl1()
{
InitializeComponent();
}
}
И ваш usercontrol xaml выглядит следующим образом ..
<UserControl x:Class="WpfApplication1.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Name="Root">
<StackPanel>
<CheckBox IsChecked="{Binding IsChecked,ElementName=Root}"></CheckBox>
<TextBlock Text="{Binding Text,ElementName=Root}"></TextBlock>
</StackPanel>
</UserControl>
Если вы заметили, у меня есть флажок и текстовое поле в привязке к свойствам зависимости, которые я создал в пользователеcontrol.
когда вы используете пользовательский контроль, вы можете связать DependencyProperty
<Grid>
<local:UserControl1 IsChecked="{Binding yourBinding}" Text="{Binding yourBinding}"/>
</Grid>