這裏是一個小樣本(我希望這是你想做的事)...
編輯:我終於編輯我的回答提供需要的屬性路徑屬性。如果任何人對此問題有其他解決方案,請讓我知道!
添加依賴項屬性的綁定路徑:
首先,我們創建一個虛擬模型,提供了標題爲我們的按鈕:
public class SampleModel
{
public string Title { get; set; }
}
然後,用戶控件。這裏最重要的是ItemsSource-Binding(ElementName = UserControl)。否則,您將綁定到Parent-DataContext中的UserControlItemsSource。
編輯:自從我上次回答以來,按鈕發生了變化!
<UserControl
x:Class="StackOverflow.ListUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StackOverflow"
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"
x:Name="SampleUserControl">
<Grid>
<ListView ItemsSource="{Binding UserControlItemsSource, ElementName=SampleUserControl}" Background="DeepSkyBlue" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<Button Loaded="FrameworkElement_OnLoaded"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</UserControl>
...和用戶控件的代碼隱藏包括UsercontrolItemsSource:
編輯:我添加了依賴屬性BindingPropertyPath和FrameworkElement_OnLoaded方法,因爲我最後的答案。
public sealed partial class ListUserControl : UserControl
{
public ObservableCollection<SampleModel> UserControlItemsSource
{
get { return (ObservableCollection<SampleModel>)GetValue(UserControlItemsSourceProperty); }
set { SetValue(UserControlItemsSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for UserControlItemsSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UserControlItemsSourceProperty =
DependencyProperty.Register("UserControlItemsSource", typeof(ObservableCollection<SampleModel>), typeof(ListUserControl), new PropertyMetadata(null));
public string BindingPropertyPath
{
get { return (string)GetValue(BindingPropertyPathProperty); }
set { SetValue(BindingPropertyPathProperty, value); }
}
// Using a DependencyProperty as the backing store for BindingPropertyPath. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindingPropertyPathProperty =
DependencyProperty.Register("BindingPropertyPath", typeof(string), typeof(ListUserControl), new PropertyMetadata(string.Empty));
public ListUserControl()
{
this.InitializeComponent();
}
private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
Binding contentBinding = new Binding();
contentBinding.Path = new PropertyPath(this.BindingPropertyPath);
button.SetBinding(Button.ContentProperty, contentBinding);
}
}
現在我們的用戶控件添加到我們的主頁(ListPageHost):
編輯:設置新的依賴屬性BindingPropertyPath到的ItemsSource你想使用的的屬性的名稱按鈕。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Add Item to ItemsSource" Click="ButtonBase_OnClick"></Button>
<local:ListUserControl Grid.Row="1" UserControlItemsSource="{Binding SampleCollection}" BindingPropertyPath="Title"/>
</Grid>
在炫魅的代碼背後,我們宣佈我們的炫魅廣東 - 視圖模型(ListPageHostViewModel):
public class ListPageHostViewModel
{
private readonly ObservableCollection<SampleModel> _sampleCollection = new ObservableCollection<SampleModel>();
public ObservableCollection<SampleModel> SampleCollection
{
get { return _sampleCollection; }
}
}
...和的MainPage的(ListPageHost)後面的代碼:
public sealed partial class ListPageHost : Page
{
public ListPageHost()
{
this.InitializeComponent();
this.DataContext = new ListPageHostViewModel();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var vm = this.DataContext as ListPageHostViewModel;
if (vm != null)
{
vm.SampleCollection.Add(new SampleModel() { Title = string.Format("new item {0}", DateTime.Now.Ticks)});
}
}
}
希望這是你在找什麼。如果您有任何問題 - 請告訴我。 亞歷克斯
嗨亞歷克斯..感謝您的回覆。該樣本非常接近我所尋找的,但需要更多的幫助。 –
嗨亞歷克斯..感謝您的回覆。該樣本非常接近我所尋找的,但需要更多的幫助。在上面的示例中,您在UserControl的按鈕內容和主頁SampleCollection中的SampleModel類型中使用了Title屬性,但是我想爲Title創建一個依賴項屬性,以便我的用戶控件的客戶端可以映射到依賴項屬性。我不想讓UserControl和MainPage知道標題屬性。我只是想讓用戶控件鬆散耦合。這是可以實現的嗎?請讓我知道。並非常感謝您的幫助。真的很有幫助。 –
我很快就會看到你的問題......我現在不在辦公室。 – Alexander