在我的mainwindow.xaml中,我得到了网格:
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Column="2" Grid.Row="0">
<local:_13cap/>
</CustomControl:GridControl>
13cap是我的自定义控件:
<UserControl x:Class="TTTP._13cap"
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:CustomControl="clr-namespace:WpfApplication1.CustomControl"
mc:Ignorable="d">
<CustomControl:GridControl ShowCustomGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="ВСЕГО" />
<CustomControl:GridControl ShowCustomGridLines="True" Grid.Column="2" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="1" Text="П" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Grid.Column="1" Grid.Row="1" Text="Ф" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Grid.Column="2" Grid.Row="1" Text="%" HorizontalAlignment="Center" VerticalAlignment="Center" />
</CustomControl:GridControl>
</CustomControl:GridControl>
</UserControl>
但是我只想用一个不同的文本参数(Text =“ВСЕГО”)来调用它,我想叫<local:_13cap MyText="CustomText"/>
覆盖“ВСЕГО”。如何在其中创建带有此类参数的UserControl?
答案 0 :(得分:3)
Implement a DependancyProperty您可以从代码隐藏更改并绑定到XAML。
代码背后:
public class MyUserControl : UserControl, INotifyPropertyChanged
{
//INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
//The XAML binding uses this
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(MyUserControl),
new PropertyMetadata(string.Empty, OnCaptionPropertyChanged));
//Your code-behind uses this
public string Caption
{
get { return GetValue(CaptionProperty).ToString(); }
set
{
SetValue(CaptionProperty, value);
OnPropertyChanged("Caption");
}
}
的Xaml:
<UserControl (...)>
<Grid>
<Label x:Name="labCaption" Content="{Binding Caption}"/>
</Grid>
</UserControl>
关于这一点还有很多其他问题,谷歌的许多好文章和教程都能解释得更好。