如何通过使用XAML中的DataTrigger更改菜单内容? [英] How to change the menu content by using DataTrigger in XAML?

查看:60
本文介绍了如何通过使用XAML中的DataTrigger更改菜单内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据登录名,我有两种菜单项.因此,通过使用ViewModel类中的属性

I have two kinds of menu items according to the login. So, by using the property in the ViewModel Class

bool IsAdmin {get; set;}

我必须更改菜单项的内容.我对数据模板不熟悉.我想在xaml本身中定义所有菜单项(可能正在使用数据模板). 我们如何使用数据触发器来绑定不同的菜单项. 任何人都可以为此举一个较小的例子.仅使用此属性,不使用c#代码.

I have to change the menu item content.I am not familiar with data template. I want to define all the menu items in the xaml itself (might be using the data templates). How we can bind the differnt menu items by using the data trigger. Can anyone can give a smaller example for this. using only this property and no c# codes.

推荐答案

使用 ContentControl Styles 可以最大程度地灵活地更改视图(不是管理员视图) >

Use ContentControl and Styles for max flexability in changing the view betwin Admin or not Admin views

<UserControl.Resources>
    <!--*********** Control templates ***********-->
    <ControlTemplate x:Key="ViewA">
        <Views:AView/>
    </ControlTemplate>
    <ControlTemplate x:Key="ViewB">
        <Views:BView />
    </ControlTemplate>
</UserControl.Resources>

<Grid>
    <ContentControl DataContext="{Binding}" Grid.Row="1">
        <ContentControl.Style>
            <Style TargetType="ContentControl">
                <Setter Property="Template" Value="{StaticResource ViewA}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsAdmin}" Value="True">
                        <Setter Property="Template" Value="{StaticResource ViewB}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl >
</Grid>  

请记住,您必须实施 VM 上的> INPC 界面,以便能够更改状态(如果不是,则仅接受一次更改(在创建具有IsAdmin属性的类时).这是INPC的实现示例:

Please keep in mind that you will have to implement the INPC interface on your VM in order to be able to change the state (is admin or not) on the fly.If not the change will be accepted only once(on the creation of the class that is holding the IsAdmin property). Here is the INPC implementation example:

public class UserControlDataContext:BaseObservableObject
{

    private bool _isAdmin;

    public bool IsAdmin
    {
        get { return _isAdmin; }
        set
        {
            _isAdmin = value;
            OnPropertyChanged();
        }
    }
}

/// <summary>
/// implements the INotifyPropertyChanged (.net 4.5)
/// </summary>
public class BaseObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
    {
        var propName = ((MemberExpression)raiser.Body).Member.Name;
        OnPropertyChanged(propName);
    }

    protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(name);
            return true;
        }
        return false;
    }
}

这篇关于如何通过使用XAML中的DataTrigger更改菜单内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆