【发布时间】: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