- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 ComboBox
绑定(bind)到我的 ViewModel 上的一个属性(从“VM”上听到)。当用户在 ComboBox
上进行选择时它会正确更新我的 VM 中的绑定(bind)属性。在我的 UI 代码中,我订阅了 PropertyChanged
我的虚拟机上的事件。
正如它应该表现的那样,当用户在 ComboBox
中进行选择时, 我的 PropertyChanged
事件在我的 UI 后端代码中正确执行。当 UI 代码捕捉到此属性的更改时,在某些选择条件下,我需要停止进程并请求用户提供更多信息。从 UI 中,我向他们发送了一个对话框。如果他们取消对话框,我会重置与 ComboBox
关联的 VM 中的值。控制 SelectedValue
.
这是我观察到的。当用户取消操作时,我的 VM 属性被设置为新值。但是,ComboBox
仍然显示现在已更改的原始条目的文本值。如何强制 ComboBox
从我的PropertyChanged
中更新自己事件?在这种情况下,我认为这只是一个文本问题或数字索引更改,它引用了绑定(bind)集合中的文本数据。 VM 中的数据正确,但显示值为 ComboBox
是错的。
示例
组合框详情
<ComboBox
ItemsSource="{Binding ListOfComboBoxDisplayObjects}"
SelectedValue="{Binding MySelectionIsAnEnumeration}"
DisplayMemberPath="Text"
SelectedValuePath="EnumerationValue"
Height="27" />
ListOfComboBoxDisplayObjects
collection 表示一组枚举器值,它们存储在
SelectedValuePath
内的路径中。 .每个值的描述性文本来自
ListOfComboBoxDisplayObjects
这是一个为 UI 严格创建的特殊列表。这基本上将枚举值与有意义的描述配对。
private ObservableCollection<BindableEnumerationItem<Values>> _listOfComboBoxDisplayObjects;
public ObservableCollection<BindableEnumerationItem<Values>> ListOfComboBoxDisplayObjects
{
get { return _listOfComboBoxDisplayObjects; }
private set
{
if (value != _listOfComboBoxDisplayObjects)
{
_listOfComboBoxDisplayObjects= value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(ListOfComboBoxDisplayObjects)));
}
}
}
private Values_mySelectionIsAnEnumeration ;
public Values MySelectionIsAnEnumeration
{
get { return _mySelectionIsAnEnumeration; }
set
{
//Double-checked this-- value is different on the second-call to change this value, once the UI cancels the operation.
if (value != _mySelectionIsAnEnumeration)
{
_mySelectionIsAnEnumeration= value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(MySelectionIsAnEnumeration )));
}
}
}
private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "MySelectionIsAnEnumeration":
if (VM.MySelectionIsAnEnumeration == Values.Baz)
{
//Prompt the user and get DialogResult.
bool answerGiven = AskAQuestionAndGetAResult();
if(!answerGiven)
VM.MySelectionIsAnEnumeration = Values.Foo;
}
break;
}
}
VM.MySelectionIsAnEnumeration
value 确实正在更改为
Value.Foo
的正确值当用户在
AskAQuestionAndGetAResult()
内取消操作时.然而,在它完成后
ComboBox
还是写着“This is Baz. I need to ask a question才可以使用。”,这显然是与
Value.Baz
关联的显示值.
CombobBox
上的显示文本正确显示现在存储在
VM.MySelectionIsAnEnumeration
中的值?
BindableEnumerationItem
的代码我在我的 Observable Collections 中用于组合框和列表框。这在我的整个应用程序中都在更简单的情况下使用,并且没有引起任何问题。请注意,这是我实际的、未更改的代码。我没有重命名任何东西。我的组合框可以绑定(bind)到每个
Item
类型安全属性的属性和
DisplayText
是描述 rune 本。
public class BindableEnumerationItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private T _item;
public BindableEnumerationItem(T item, string displayText)
{
_item = item;
_displayText = displayText;
}
private string _displayText;
public string DisplayText
{
get { return _displayText; }
set
{
if (value != _displayText)
{
_displayText = value;
PropertyChanged(this, new PropertyChangedEventArgs("DisplayText"));
}
}
}
public T Item
{
get { return _item; }
set
{
_item = value;
PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
}
最佳答案
创建一个扩展,将 xaml 中的 View 模型中的命令连接到选择器,在本例中为组合框。
public partial class Extensions
{
public static readonly DependencyProperty SelectionChangedCommandProperty = DependencyProperty.RegisterAttached("SelectionChangedCommand", typeof(ICommand), typeof(Extensions), new UIPropertyMetadata((s, e) =>
{
var element = s as Selector;
if (element != null)
{
element.SelectionChanged -= OnSelectionChanged;
if (e.NewValue != null)
{
element.SelectionChanged += OnSelectionChanged;
}
}
}));
public static ICommand GetSelectionChangedCommand(UIElement element)
{
return (ICommand)element.GetValue(SelectionChangedCommandProperty);
}
public static void SetSelectionChangedCommand(UIElement element, ICommand value)
{
element.SetValue(SelectionChangedCommandProperty, value);
}
private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var element = sender as Selector;
var command = element.GetValue(SelectionChangedCommandProperty) as ICommand;
if (command != null && command.CanExecute(element.SelectedItem))
{
command.Execute(element.SelectedItem);
e.Handled = true;
}
}
}
public ICommand EnumerationValueChangedCommand
{
get
{
return new Command(
() =>
{
if (VM.MySelectionIsAnEnumeration == Values.Baz)
{
//Prompt the user and get DialogResult.
bool answerGiven = AskAQuestionAndGetAResult();
if (!answerGiven)
VM.MySelectionIsAnEnumeration = Values.Foo;
}
});
}
}
<ComboBox
ItemsSource="{Binding ListOfComboBoxDisplayObjects}"
SelectedValue="{Binding MySelectionIsAnEnumeration}"
DisplayMemberPath="Text"
SelectedValuePath="EnumerationValue"
ext:Extensions.SelectionChangedCommand="{Binding EnumerationValueChangedCommand}"
Height="27" />
关于c# - 如何从 PropertyChanged 事件中正确重置与 ComboBox 关联的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958534/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我有一个包含一些数据的网格(用户列表)。对于每一行,我有许多操作,例如更新、删除、激活、暂停、查看您命名的订单。 而不是放置如此多的按钮,这些按钮将填充超过 400-500像素 我想放置一个组合框,其
在我的一个对话框中,我有以下控制: 我在其他地方填充 ComboBox,如下所示: 但是,如果我没有创建 ComboBox 位,MSI 仍将构
我在项目中为 MVC 使用了 kendo complete。 我有某些形式的国家/地区列表,我显示国家/地区名称,但存储国家/地区代码。 我有以下问题:当用户输入不在列表中的内容时,该值将发送到服务器
我有一个组合框,其中的值是从托管 bean 填充的,如下所示: keywordlist.setConnDB("jdbc:sqlserver://xx.xx.x.xx:1433;DatabaseName
我有一个 ComboBox,它绑定(bind)到 ViewModel 中的复杂类型集合,这些类型的长度可以是任意数量,具体取决于用户的偏好。 我已经创建了一个基于 ComboBox 默认值的样式,并且
我做了一个转换器: public class BooleanToDateConverter implements Converter { private static final long s
编辑:由于 Rob 的回答,我已经更新了下面的代码,现在它可以工作了。 我找到了几页显示如何执行此操作的页面( http://www.cmcrossroads.com/content/view/131
我是 PyQT 的新手。 我有兴趣向 tableView 的每一行添加一个组合框。在 PyQT 4 中可能吗? 我知道,在 QT5 中是可能的,但不确定 PyQT。 预先感谢您的帮助。 最佳答案 如果
我对 JavaFX(8)、HBox、ComboBox 和 HGrow 有问题。 HGrow 不能与 ComboBox 结合使用。 (信息:使用 TextField (而不是 ComboBox),它按预
我有一个 XAML UserControl连接到 ImportPresenter View 模型。有四个ComboBox我的 XAML 中的项目: CashActivityTypeBAI CashAc
我将两个组合框绑定(bind)到同一个 listviewcollection。问题是在一个组合框中选择一个值会导致另一个组合框选定项更改为第一个组合框的确切值。它们是耦合的,我希望它们彼此独立。 My
我正在尝试在 extjs 3.4 中的组合框中的选项之间添加一行。我可以添加该行,但不能用我的远程位置的数据填充它。 (如果我删除修改的 tpl 选项,它就会填充)。 这是我的代码。我只需要在“组”字
我被 WIX 安装程序中的组合框和自定义操作困住了。 我有一个包含几个值的组合框(下拉菜单)。当用户从此下拉列表中选择一个值时,我想在屏幕上显示一些文本(对于下拉列表中的每个项目都是唯一的)。 在 .
我有 ComboBox cbx 和一个包含选项卡的 TabPane(选项卡:t)和一个按钮 b1。因此,单击此按钮 b1 时,它会在 TabPane 中添加一个新选项卡 t,并在包含以下内容的 Com
我有两个组合框:水果和饮料。 fruits 具有字符串:“apple”、“orange”、“banana” drinks 具有字符串:“water”、“coffee”、“juice” 如何制作一个新组
我必须监听什么事件,以便在用户从(可编辑的)WPF ComboBox control 中选择一个选项时得到通知? 我是否必须先访问 Items 属性才能收听 Items.CurrentChanged?
我有以下简单的 QML 组合框: import QtQuick 2.0 import QtQuick.Controls 1.4 import si.mikroelektronika 1.0 Item
当我创建组合框时,列表中没有项目。现在,当我单击下拉按钮时,会调用一个函数(通过 postcommand 选项),但是一旦在我的函数中,我不知道如何在组合框的列表框中设置值。 代码如下: #u
我有两个组合框 我使用 LINQ-to-Entities 来填充 cmbGroup 组合框 Dim db as myDataEntity cmbGroup.ItemsSource = db.Mak
我是一名优秀的程序员,十分优秀!