【问题标题】:WPF Passing DataSource into a UserControlWPF将数据源传递给用户控件
【发布时间】:2017-08-03 14:54:52
【问题描述】:

我有一个非常简单的用户控件:

<UserControl x:Class="PointOfSale.UserControls.HousesGrid"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<ItemsControl x:Name="LayoutRoot" ItemsSource ="{Binding PopularHouses}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="5"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ToggleButton
                Content="{Binding FormattedPanelTimeRemaining}"
                Style="{StaticResource MetroToggleButtonStyle}"
                Height="45"
                Width="80"
                VerticalAlignment="Center"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如您所见,ItemSource 属性绑定到父 ViewModel 上的 PopularHouses 属性。这很好用。但是,我想要做的是将 LayoutRoot 元素的 ItemSource 设置为父窗体上将控件插入 XAML 的位置的不同属性。

最终结果应该是这个用户控件的多个实例,绑定到父数据上下文的几个不同属性。

有人能解释一下如何实现吗?

【问题讨论】:

  • 你意识到你在向后思考,对吧?为什么要更改您绑定的属性,而不是让您的视图模型通用?
  • 如果您希望ItemsSource 从外部可绑定,您可以将其公开为用户控件的依赖属性并简单地将LayoutRoot.ItemsSource 绑定到它。见this answer

标签: c# wpf xaml user-controls


【解决方案1】:

您只需使用RelativeSource 将您的UserControl 的DataContext 绑定到第一个ContentControl 的数据上下文。

 DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}"

我做了以下示例:

XAML 主窗口

<Window x:Class="WpfDataContext.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDataContext"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <local:UserControl1/>
    </Grid>
</Window>

仅出于本示例的目的,我们将其 datacontext 设置为 Self。在代码隐藏中,我们定义了一个简单的属性来展示它是如何工作的:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public string SomeString { get; set; } = "Hello";
}

然后,用户控件 XAML:

<UserControl x:Class="WpfDataContext.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}">
    <Grid>
        <TextBlock Text="{Binding SomeString}"/>
    </Grid>
</UserControl>

注意我们如何绑定它的 DataContext 属性,因为这是关键。

为了简单起见,我使用 Textblock,但该原则也适用于您的情况

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 2012-03-21
    • 2011-05-31
    • 1970-01-01
    • 2014-03-10
    • 2013-10-18
    • 2010-12-15
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多