XAML如何使用DataTrigger改变菜单内容?
How to change the menu content by using DataTrigger in XAML?
我根据登录有两种菜单项。因此,通过在 ViewModel Class
中使用 属性
bool IsAdmin {get; set;}
我必须更改菜单项content.I 我不熟悉数据模板。我想在 xaml 本身中定义所有菜单项(可能正在使用数据模板)。
我们如何使用数据触发器绑定不同的菜单项。
谁能为此举一个更小的例子。仅使用此 属性 且不使用 c# 代码。
使用 ContentControl 和 Styles 以最大程度地灵活地在 Admin 或非 Admin 视图之间更改视图
<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>
请记住,您必须实施 INPC interface on your VM 才能更改 fly.If 上的状态(是否为管理员),而不是更改只会被接受一次(在创建持有 IsAdmin 属性 的 class 时)。下面是 INPC 的实现示例:
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;
}
}
我根据登录有两种菜单项。因此,通过在 ViewModel Class
中使用 属性bool IsAdmin {get; set;}
我必须更改菜单项content.I 我不熟悉数据模板。我想在 xaml 本身中定义所有菜单项(可能正在使用数据模板)。 我们如何使用数据触发器绑定不同的菜单项。 谁能为此举一个更小的例子。仅使用此 属性 且不使用 c# 代码。
使用 ContentControl 和 Styles 以最大程度地灵活地在 Admin 或非 Admin 视图之间更改视图
<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>
请记住,您必须实施 INPC interface on your VM 才能更改 fly.If 上的状态(是否为管理员),而不是更改只会被接受一次(在创建持有 IsAdmin 属性 的 class 时)。下面是 INPC 的实现示例:
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;
}
}