在 Silverlight 中创建 LoginStatusControl

发布于 2024-08-03 22:38:38 字数 2024 浏览 2 评论 0原文

我正在尝试在 silverlight 中创建一个登录状态控件,其中我将使用多个 ControlTemplates 来定义条件内容。

到目前为止,我已经创建了一个 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。

在搜索时,我发现这个问题并尝试使用模板绑定但我无法让它发挥作用。

如果用户登录与否,是否有办法显示此条件模板?还有另一种方法可以解决我所缺少的这个问题吗?我希望提出一个解决方案,可以在控件的 DataContext 更改时动态更新模板。

I'm trying to create a login status control in silverlight where I will use multiple ControlTemplates to define conditional content.

So far I have created a 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;
        }
    }
}

Then I've defined the templates in a Style.

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

I'm having difficulty getting the conditional templates connected to override the ControlTemplate.

While searching I found this question and tried to use template binding but I couldn't get that to work.

Is there anyway to get this conditional templates to display if the user is logged in or not? Is there another way of solving this problem that I'm missing? I'm hoping to come up with a solution that can update the template dynamically when the DataContext of the control changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2024-08-10 22:38:38

好吧,我最终选择了 ContentContent 的 Content 属性并提供条件 DataTemplate。

这是控件:

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>

Well, I ended up going with a ContentContent's Content property and providing conditional DataTemplates.

Here is the Control:

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

And here is the Default Style.

 <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文