这是Windows Phone 8.1(运行时)Windows Phone CustomControl数据绑定
我有一些绑定自定义用户controll与数据列表的问题。我会尽可能简化。
我的问题是,如果我在自定义controll中使用DataBind {Binding Something},它将无法工作。
我需要将绑定数据(字符串)传输到自定义控件。
奇怪的是,如果我不使用DataBind,它将正常工作。例如MyCustomControllParameter =“一些字符串”(在我的示例'BindingTextValue'属性中)
有谁知道如何将自定义用户controll与DataTemplate内部的ListView绑定。
假设这样的:
XAML试验主页
<Grid Background="Black">
<ListView x:Name="TestList" Background="#FFEAEAEA">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="#FF727272">
<local:TextBoxS BindingTextValue="{Binding Tag, FallbackValue='aSource'}" local:TextBoxS>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
XAML试验主页C#
public sealed partial class MainPage : Page
{
List<TTag> tags = new List<TTag>();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
public class TTag
{
public string Tag { get; set; }
}
private void InitializeAppData()
{
TTag tag = new TTag() { Tag = "hello world" };
tags.Add(tag);
tags.Add(tag);
tags.Add(tag);
TestList.ItemsSource = tags;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
InitializeAppData();
}
}
用户控制XAML:
<UserControl
x:Class="CustomControllTest.TextBoxS"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CustomControllTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="#FF4F4F4F" >
<RichTextBlock x:Name="MyTestBlock">
</RichTextBlock>
</Grid>
用户控制C#的.cs
public TextBoxS()
{
this.InitializeComponent();
LayoutRoot.DataContext = this;
}
public static readonly DependencyProperty BindingTextValueProperty = DependencyProperty.Register(
"BindingTextValue",
typeof(string),
typeof(TextBoxS),
new PropertyMetadata(default(string)));
public string BindingTextValue
{
get
{
return GetValue(BindingTextValueProperty) as string;
}
set
{
SetValue(BindingTextValueProperty, value);
//This method adds some custom logic into RichTextBlock, pointed correctly
SetupSpotterBox(value);
}
}
感谢您的帮助;)
没有probs不会忘记标记为答案,如果它解决了问题或没有,如果它没有;) – SWilko 2015-02-24 13:40:26