English 中文(简体)
• 在银灯上建立LoginStatusControl
原标题:Creating a LoginStatusControl in Silverlight
  • 时间:2009-09-01 00:58:53
  •  标签:

我试图在银灯上建立标识地位控制,在这种控制下,我将使用多个控制模板来界定有条件的内容。

至今,我已成立了洛辛斯塔图人委员会。

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>

很难找到与控制模板相连接的有条件模板。

在查询时,我发现,并试图使用具有约束力的模板,但我不得不这样做。

如果用户不作标记,是否有什么办法显示这种有条件的模板? 是否有另一种办法解决我失踪的问题? 我希望提出一个解决办法,以便在控制变化的数据内容发生变化时能够动态地更新模板。

最佳回答

简而言之,我最后采用了<代码>Content s content 财产并提供有条件的数据模板。

这里的控制:

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>
问题回答

暂无回答




相关问题
热门标签