WPF数据绑定UserControl

时间:2017-04-13 07:35:37

标签: wpf xaml mvvm data-binding

我是WPF的新手,我使用的是MVVM模式。

我有一个带网格的WPF UserControl。 Grid有两列。第一个用于选择目的,第二个用于显示另一个UserControl。

我知道如何在.xaml中显示用户控件:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*" />
        <ColumnDefinition Width="6*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBlock Text="Cellular View" Grid.ColumnSpan="2" Style="{StaticResource UXHeaderTextStyle}"></TextBlock>
    <local:ViewBillingLevel Grid.Row="1" Grid.Column="1" Visibility="{Binding  BillingLevelVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewCity Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewCityVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewClientSite Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewClientSiteVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewCompany Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewCompanyVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewCompanyBillingLevel Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewCompanyBillingLevelVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewCompanyGroup Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewCompanyGroupVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewContractService Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewContractServiceVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewDeviceMake Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewDeviceMakeVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewDeviceModel Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewDeviceModelVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewPackage Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewPackageVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewProvince Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewProvinceVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewServiceProvider Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewServiceProviderVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewStatus Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewStatusVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
    <local:ViewSuburb Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewSuburbVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>
</Grid> 

但是现在我想要使用不同的UserControls进行数据绑定。

此时我唯一的选择是为我想要使用的每个UserControl创建一个标记,并将Data绑定到它们的Visibility属性。

但是从编程的角度来看,它会占用大量内存,因此我正在寻找一种更有效的方法来实现它。

任何帮助都将不胜感激。

感谢。

编辑(我当前的解决方案):

<local:ViewClientSite Grid.Row="1" Grid.Column="1" Visibility="{Binding  ViewClientSiteVisibility}" Width="Auto" Height="Auto" Margin="{Binding UserControlMargin}"/>

但就像我说当我打开这个窗口时它会滞后因为它加载了所有UserControls。如果有可能我想要其中一个并将不同的UserControl绑定到它:

{{1}}

1 个答案:

答案 0 :(得分:0)

您可以放置​​将绑定到将托管ViewModel的属性的ContentPresenter,而不是一次创建所有这些内容。

在主页面下,为每个viewmodel定义DataTemplate(使用DataType而不是x:Key),每个都将托管另一个UserControl。