2009-07-27 45 views
0

我有一個要求,基於登錄到我的應用程序的用戶,我需要顯示不同的視圖。我怎樣才能達到這個使用數據模板?WPF中的動態數據模板

感謝, Jithu

+0

嗨,你能更具體一點嗎?這是Windows用戶帳戶還是特定於應用程序的帳戶(即您的應用程序是否維護用戶列表)?是否有用於確定視圖的帳戶的特定屬性?你是什​​麼類型的視圖/控制? – 2009-07-27 18:34:41

回答

0

假設你有資源字典中類似這樣爲每個用戶組/類型的WPF項目的根:

  • UserOneResources.xaml
  • UserTwoResources.xaml
  • ...

其中包含的DataTemplates :

<!-- UserOneResources.xaml --> 
<DataTemplate DataType="{x:Type s:String}"> 
    <TextBlock Text="{Binding .}" /> 
</DataTemplate> 

<!-- UserTwoResources.xaml --> 
<DataTemplate DataType="{x:Type s:String}"> 
    <TextBox Text="{Binding .}" /> 
</DataTemplate> 

然後在你的App.xaml.cs的構造函數,你可以選擇合適的資源字典爲當前用戶類型如下所示:

public App() 
{ 
    string resourceDictionaryToUse; 

    if (user.Type = UserType.One) 
    { 
     resourceDictionaryToUse = "UserOneResources.xaml"; 
    } 
    else 
    { 
     resourceDictionaryToUse = "UserTwoResources.xaml"; 
    } 

    var rd = new ResourceDictionary() { Source = new Uri("pack://application:,,,/" + resourceDictionaryToUse) }; 

    this.Resources.MergedDictionaries.Add(rd); 
} 

希望這有助於。