0

在我的 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?

4

1 回答 1

3

实现一个 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");
        } 
    }

xml:

<UserControl (...)>
    <Grid>
        <Label x:Name="labCaption" Content="{Binding Caption}"/>
    </Grid>
</UserControl>

这里还有很多关于这个的其他问题,还有很多关于谷歌的好文章和教程,它们比我能更好地解释它。

于 2012-04-07T12:14:29.673 回答