我的界面显示AD用户列表,以及每个名称旁边的一对按钮以禁用/启用该用户。每个按钮都绑定到适当的DelegateCommand。点击“启用”按钮按钮禁用该按钮并启用另一个按钮。用户可能会被其他用户通过其他方式启用/禁用,我希望这会反映在我的应用中。
我尝试过的事情:
RaisePropertyChangedEvent("CommandEnable");
RaisePropertyChangedEvent("CommandDisable");
我正在使用MVVM。
我已经设置了一个Dispatch计时器,以便每秒触发上述行。
编辑:我似乎通过更新整个用户列表并将其重新指定为我的ListBox的ItemSource来解决了这个问题。
我无法帮助,但觉得这很麻烦。有没有办法只触发其已启用属性已更改的用户的更新?在我的模型中
public class UserItem //The model
{
private UserPrincipal _User;
public string SamAccountName {
get
{
return _User.SamAccountName;
}
}
public UserItem(UserPrincipal user)
{
_User = user;
}
public bool Enabled
{
get
{
if (_User.Enabled != null)
{
return (bool)_User.Enabled;
} else
{
return false;
}
}
set
{
_User.Enabled = value;
_User.Save();
}
}
}
在我的ViewModel中:
public class UserVM : ObservableObject, IComparable //ViewModel
{
private UserItem _userItem;
private ISet<String> _Groups = new HashSet<String>();
public string SamAccountName
{
get
{
return _userItem.SamAccountName;
}
}
public UserVM(UserPrincipal user, ISet<String> groups)
{
_userItem = new UserItem(user);
_Groups = groups;
}
public bool IsInGroup(String groupName)
{
return (_Groups.Contains(groupName)) ? true : false;
}
public bool Enabled
{
get
{
return _userItem.Enabled;
}
set
{
_userItem.Enabled = value;
RaisePropertyChangedEvent("Enabled");
RaisePropertyChangedEvent("CommandEnable");
RaisePropertyChangedEvent("CommandDisable");
}
}
private void EnableUser(object state)
{
Enabled = true;
}
private bool CanEnable(object state)
{
return !Enabled;
}
private void DisableUser(object state)
{
Enabled = false;
}
private bool CanDisable(object state)
{
return Enabled;
}
public int CompareTo(object comp)
{
return this._userItem.SamAccountName.CompareTo(((UserVM)comp).SamAccountName);
}
public ICommand CommandEnable
{
get {
return new DelegateCommand(EnableUser, CanEnable);
}
}
public ICommand CommandDisable
{
get { return new DelegateCommand(DisableUser, CanDisable); }
}
}
我的XML:
<Window x:Class="Toggle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Toggle"
mc:Ignorable="d"
Title="MainWindow" Height="512" Width="339">
<Window.Resources>
</Window.Resources>
<Grid Margin="10">
<ListBox Name="lbUserList" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,22,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding SamAccountName}" />
<Button Name="btnEnable" Grid.Column="1" Content="Enable" Background="Green" Command="{Binding CommandEnable}"/>
<Button Name="btnDisable" Grid.Column="2" Content="Disable" Background="Red" Command="{Binding CommandDisable}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ComboBox Name="cbGroupList" SelectedIndex="0" DropDownClosed="ComboBox_DropDownClosed" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="Auto"/>
</Grid>
</Window>
此外,此轮询会干扰用户点击按钮。如果UI更新发生在用户单击按钮的同一时间,则单击按钮不会注册。我的直觉告诉我,这是一个不可解决的竞争条件(用户 - 作家冲突),但我希望我错了。