MahApps.Metro SplitButton SelectedItem 数据绑定
MahApps.Metro SplitButton SelectedItem Databinding
视图是:
<Controls:SplitButton Margin="217,409.75,56,185" Name="SplitButton1"
Width="384"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Orientation="Vertical"
DisplayMemberPath ="UserName"
SelectedItem="{Binding SelectedUser,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
ItemsSource="{Binding Users, Mode=TwoWay}" />
ViewModel 是:
public string SelectedUser
{
get { return selectedUser; }
set
{
selectedUser = value;
RaisePropertyChanged("SelectedUser");
}
}
public ObservableCollection<UserModel> Users
{
get
{
return users;
}
set
{
users = value;
}
}
型号是:
public class UserModel
{
private int id;
private string userName;
private int groupId;
private string deviceMacAddress;
public int Id { get; set; }
public string UserName { get; set; }
public int GroupId { get; set; }
public string DeviceMacAddress { get; set; }
}
我在xaml中使用上面的代码将splitbutton中的selectedItem绑定到ViewModel->属性--SelectedUser.
但它不起作用。有人知道为什么吗?
SelectedUser 作为模型名称 (PresentationLayer.Model.UserModel) 而不是 UserName 属性返回。
因为根据您的定义,您的绑定是 OneWay
。
将绑定设置为 TwoWay
。
<Controls:SplitButton SelectedItem="{Binding SelectedUser,Mode=TwoWay}"/>
而且,在这种情况下不需要设置UpdateSourceTrigger=PropertyChanged
,因为UpdateSourceTrigger
默认为PropertyChanged
SelectedItem
属性 .
视图是:
<Controls:SplitButton Margin="217,409.75,56,185" Name="SplitButton1"
Width="384"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Orientation="Vertical"
DisplayMemberPath ="UserName"
SelectedItem="{Binding SelectedUser,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
ItemsSource="{Binding Users, Mode=TwoWay}" />
ViewModel 是:
public string SelectedUser
{
get { return selectedUser; }
set
{
selectedUser = value;
RaisePropertyChanged("SelectedUser");
}
}
public ObservableCollection<UserModel> Users
{
get
{
return users;
}
set
{
users = value;
}
}
型号是:
public class UserModel
{
private int id;
private string userName;
private int groupId;
private string deviceMacAddress;
public int Id { get; set; }
public string UserName { get; set; }
public int GroupId { get; set; }
public string DeviceMacAddress { get; set; }
}
我在xaml中使用上面的代码将splitbutton中的selectedItem绑定到ViewModel->属性--SelectedUser.
但它不起作用。有人知道为什么吗?
SelectedUser 作为模型名称 (PresentationLayer.Model.UserModel) 而不是 UserName 属性返回。
因为根据您的定义,您的绑定是 OneWay
。
将绑定设置为 TwoWay
。
<Controls:SplitButton SelectedItem="{Binding SelectedUser,Mode=TwoWay}"/>
而且,在这种情况下不需要设置UpdateSourceTrigger=PropertyChanged
,因为UpdateSourceTrigger
默认为PropertyChanged
SelectedItem
属性 .