0

我有使用用户控件生成 gridview 的代码:

 <ListView x:Name="ListView" SelectionMode="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                            <UserControls:ItemTemplateControl Parametr="XXXXXXX"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Listview 工作正常,但我无法向我的 UserControl 发送任何内容。用户控制代码:

public sealed partial class ItemTemplateControl : UserControl
    {
        public string Parametr
        {
            get
            {
                return (string)GetValue(ParametrProperty);
            }
            set
            {
                SetValue(ParametrProperty, value);
            }
        }
        public static DependencyProperty ParametrProperty = DependencyProperty.Register("Parametr", typeof(string), typeof(ItemTemplateControl), new PropertyMetadata(""));


        public ItemTemplateControl()
        {
            this.InitializeComponent();
            post_text.Text = Parametr;
            Get();
        }

此代码不起作用!我现在不知道问题出在哪里?

对不起我的英语不好

<Page
    x:Class="App13.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App13"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:UserControls="using:App13.UserControls"
    mc:Ignorable="d">
4

1 回答 1

1

我想您想在依赖属性Parametr更改时注册一个回调。为此,您必须:

public static DependencyProperty ParametrProperty = DependencyProperty.Register("Parametr", typeof(string), typeof(ItemTemplateControl), new PropertyMetadata("", ParametrChanged));

ParametrChanged并在方法中定义一些逻辑

private static void ParametrChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
       var sender = d as ItemTemplateControl;
       if (sender == null) return;

       var newValue = e.NewValue as string;
       sender.post_text.Text = newValue;
}
于 2013-10-01T10:39:31.133 回答