2017-04-13 167 views
0

我是WPF的新手,我正在使用MVVM模式。WPF數據綁定用戶控件

我有一個帶有網格的WPF UserControl。網格有兩列。第一個用於選擇目的,第二個用於顯示另一個UserControl。

我知道如何的.xaml中顯示用戶控件:

<local:ViewSecurityCF Grid.Column="1" Grid.Row="0"/> 

但現在我想做數據綁定如果可能的話用不同的用戶控件。

此時,我唯一的選擇是爲我想要使用的每個UserControl創建一個標籤,併爲它們的可見性屬性綁定數據。

但從編程的角度來看,它會消耗太多的內存,因此我正在尋找一種更有效的方式來做到這一點。

任何幫助,將不勝感激。

謝謝。

編輯(我目前的解決方案):

<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> 

但就像我說的,當我打開這個窗口就落後,因爲它加載所有的用戶控件。如果有可能,我想其中的一個與不同的用戶控件綁定到它:

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

我沒有得到你想要達到的。你可以請添加你已經做了什麼,你想實現什麼 –

回答

0

而不是創造所有的人都在一次的,你可以把將結合到將承載一個ViewModel屬性ContentPresenter。

在主頁面下,資源爲每個視圖模型(使用DataType而不是x:Key)定義DataTemplate,每個將託管另一個UserControl。

+0

謝謝,這裏有幾個鏈接,幫助我與你提供的信息。 [link1](http://stackoverflow.com/questions/15367115/load-multiple-usercontrols-on-wpf-window)[link2](http://stackoverflow.com/questions/29679847/use-binding-to-變化-用戶控件功能於一個柵) – Elmer