[英]Problems with Binding to UserControl
我有使用usercontrols生成gridview的代码:
<ListView x:Name="ListView" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<UserControls:ItemTemplateControl Parametr="XXXXXXX"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Listview工作正常,但我无法将任何内容发送到我的UserControl。 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">
我想您想为依赖属性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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.