How to change the menu content by using DataTrigger in XAML?

点点圈 提交于 2019-12-06 20:06:18
Noam M

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>  

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!