2009-09-01 5 views
0

Silverlightでログインステータスコントロールを作成しようとしていますが、ここで複数のControlTemplateを使用して条件付きコンテンツを定義します。SilverlightでLoginStatusControlを作成する

これまでのところ、私はその後、私はスタイルでテンプレートを定義したLoginStatusControl

public class LoginStatusControl : ContentControl 
{ 
    // these are actually Depedency Properties 
    public ControlTemplate LoggedInTemplate { get; set; } 
    public ControlTemplate AnonymousTemplate { get; set; } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     var user = this.DataContext as User; 
     if (user == null && this.AnonymousTemplate != null) 
     { 
      this.Template = this.AnonymousTemplate; 
     } 
     else if (this.LoggedInTemplate != null) 
     { 
      this.Template = this.LoggedInTemplate; 
     } 
    } 
} 

を作成しました。

<Style x:Key="UserStatusStyle" TargetType="local:LoginStatusControl"> 
    <Setter Property="LoggedInTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="User " /> 
        <TextBlock Text="{Binding FirstName}" /> 
        <TextBlock Text=" " /> 
        <TextBlock Text="{Binding LastName}" /> 
        <TextBlock Text=" is logged in" /> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="AnonymousTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <TextBlock Text="Please create your profile" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

ControlTemplateをオーバーライドするために条件付きテンプレートを接続するのが難しいです。

検索中にthis questionが見つかりましたが、テンプレートバインディングを使用しようとしましたが、動作させることができませんでした。

ユーザーがログインしている場合にこの条件付きテンプレートを表示するにはどうしますか?この問題を解決する別の方法がありますか?私は、コントロールのDataContextが変更されたときに動的にテンプレートを更新できるソリューションを考え出しています。

答えて

0

まあ、私はContentContent's Contentプロパティを使い、条件付きのDataTemplatesを用意しました。ここで

はコントロールです:

public class LoginStatusControl : ContentControl 
{ 
    public DataTemplate LoggedInTemplate 
    { 
     get { return (DataTemplate)GetValue(LoggedInTemplateProperty); } 
     set { SetValue(LoggedInTemplateProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for LoggedInTemplate. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty LoggedInTemplateProperty = 
     DependencyProperty.Register("LoggedInTemplate", typeof(DataTemplate), typeof(LoginStatusControl), new PropertyMetadata(null)); 


    public DataTemplate AnonymousTemplate 
    { 
     get { return (DataTemplate)GetValue(AnonymousTemplateProperty); } 
     set { SetValue(AnonymousTemplateProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for AnonymousTemplate. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty AnonymousTemplateProperty = 
     DependencyProperty.Register("AnonymousTemplate", typeof(DataTemplate), typeof(LoginStatusControl), new PropertyMetadata(null)); 


    public LoginStatusControl() 
    { 
     DefaultStyleKey = typeof(LoginStatusControl); 
    } 

    public override void OnApplyTemplate() 
    { 
     UpdateTemplate(); 

     base.OnApplyTemplate(); 
    } 

    private void UpdateTemplate() 
    { 
     var content = (ContentControl)base.GetTemplateChild("LoginControl"); 
     if (content == null) 
      return; 

     var user= this.DataContext as User; 
     if (user == null && this.AnonymousTemplate != null) 
     { 
      content.Content = this.DataContext; 
      content.ContentTemplate = this.AnonymousTemplate; 
     } 
     else if (this.LoggedInTemplate != null) 
     { 
      content.Content = this.DataContext; 
      content.ContentTemplate = this.LoggedInTemplate; 
     } 
    } 
} 

そしてここでは、デフォルトのスタイルです。

<Style x:Key="LoginStatusStyle" TargetType="controls:LoginStatusControl"> 
    <Setter Property="LoggedInTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="User: "/> 
        <TextBlock Text="{Binding FirstName}" FontWeight="Bold" /> 
        <TextBlock Text=" " /> 
        <TextBlock Text="{Binding LastName}" FontWeight="Bold" /> 
        <TextBlock Text=" is logged in" /> 
       </StackPanel> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="AnonymousTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <TextBlock Text="Please create your profile" /> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <ContentControl x:Name="LoginControl" Margin="10,0" VerticalAlignment="Center" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
関連する問題