Wpf ComboBox.SelectedValue未从绑定源更新
以下是我的绑定源对象:Wpf ComboBox.SelectedValue未从绑定源更新,wpf,data-binding,binding,combobox,Wpf,Data Binding,Binding,Combobox,以下是我的绑定源对象: Public Class MyListObject Private _mylist As New ObservableCollection(Of String) Private _selectedName As String Public Sub New(ByVal nameList As List(Of String), ByVal defaultName As String) For Each name In nameLi
Public Class MyListObject
Private _mylist As New ObservableCollection(Of String)
Private _selectedName As String
Public Sub New(ByVal nameList As List(Of String), ByVal defaultName As String)
For Each name In nameList
_mylist.Add(name)
Next
_selectedName = defaultName
End Sub
Public ReadOnly Property MyList() As ObservableCollection(Of String)
Get
Return _mylist
End Get
End Property
Public ReadOnly Property SelectedName() As String
Get
Return _selectedName
End Get
End Property
End Class
这是我的XAML:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:local="clr-namespace:WpfApplication1"
>
<Window.Resources>
<ObjectDataProvider x:Key="MyListObject" ObjectInstance="" />
</Window.Resources>
<Grid>
<ComboBox Height="23"
Margin="24,91,53,0"
Name="ComboBox1"
VerticalAlignment="Top"
SelectedValue="{Binding Path=SelectedName, Source={StaticResource MyListObject}, Mode=OneWay}"
ItemsSource="{Binding Path=MyList, Source={StaticResource MyListObject}, Mode=OneWay}"
/>
<Button Height="23"
HorizontalAlignment="Left"
Margin="47,0,0,87"
Name="btn_List1"
VerticalAlignment="Bottom"
Width="75">List 1</Button>
<Button Height="23"
Margin="0,0,75,87"
Name="btn_List2"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Width="75">List 2</Button>
</Grid>
</Window>
当窗口第一次加载时,绑定连接良好。组合框包含名称“Joe”和“Steve”,默认情况下选择“Steve”。但是,当我单击一个按钮将ObjectInstance切换到obj2时,下拉列表中的ComboBox ItemsSource将正确填充,但是SelectedValue设置为Nothing,而不是等于obj2.SelectedName。上周我们遇到了类似的问题。它与
SelectedValue
如何更新其内部有关。我们发现,如果您设置SelectedValue
,它将看不到我们必须进行的更改,而改为设置SelectedItem
,它将正确地更新所有内容。我的结论是,SelectedValue
是为get操作设计的,而不是为set设计的。但是这可能只是当前版本的3.5sp1.net中的一个bug,它遇到了类似的问题,最后我订阅了下拉列表的SelectionChanged事件,并用它设置了我的数据属性。愚蠢且希望不需要它,但它起作用了。在组合框的xaml中设置SelectedValuePath=“Content”
,然后使用SelectedValue
作为绑定是否合理
似乎您有一个字符串列表,并且希望绑定只针对组合框中的实际项目内容进行字符串匹配,因此如果您告诉它要将哪个属性用于SelectedValue
,它应该可以工作;至少,当我遇到这个问题时,这对我是有效的
对于
SelectedValue
来说,内容似乎是一个合理的默认值,但也许不是这样?绑定模式需要是单向源或双向,因为源是您想要更新的。“单向模式”是源到目标的模式,因此使源为只读模式,这将导致从不更新源。您知道。。。今天我为这个问题争论了好几个小时,你知道我发现了什么吗?这是一个数据类型问题!填充组合框的列表是Int64,我试图将该值存储在Int32字段中!没有抛出错误,只是没有存储值 要挑起一段2岁的对话:
如果要使用字符串,另一种可能是将其绑定到combobox的Text属性
<ComboBox Text="{Binding Test}">
<ComboBoxItem Content="A" />
<ComboBoxItem Content="B" />
<ComboBoxItem Content="C" />
</ComboBox>
上面的代码是双向的,因此如果设置Test=“B”;在代码中,组合框将显示“B”,如果从下拉列表中选择“A”,则绑定属性将反映更改 所选值路径的类型必须与所选值路径的类型完全相同 例如,如果
SelectedValuePath
的类型为Int16
,而绑定到SelectedValue
的属性类型为int
,则它将不起作用
我花了好几个小时才发现这一点,这就是为什么我在问了这么多时间后才在这里回答这个问题。也许另一个像我这样有同样问题的可怜人也能看到。问题:
ComboBox类使用IndexOf方法搜索指定的对象。此方法使用Equals方法确定相等性
解决方案:
因此,尝试通过以下转换器使用SelectedValue设置SelectedIndex:
C#代码
//Converter
public class SelectedToIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is YourType)
{
YourType YourSelectedValue = (YourType) value;
YourSelectedValue = (YourType) cmbDowntimeDictionary.Tag;
YourType a = (from dd in Helper.YourType
where dd.YourTypePrimaryKey == YourSelectedValue.YourTypePrimaryKey
select dd).First();
int index = YourTypeCollection.IndexOf(a); //YourTypeCollection - Same as ItemsSource of ComboBox
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value!=null && value is int)
{
return YourTypeCollection[(int) value];
}
return null;
}
}
Xaml
祝你好运!:) 您是否尝试引发一个事件,表明SelectName已更新,例如OnPropertyChanged(“SelectedName”)?这对我很有用。使用
UpdateSourceTrigger=PropertyChanged
在绑定中,刚刚解决了这个问题。呵呵!!!
使用[其中一个…]。SelectedValue |。SelectedItem |。SelectedText
提示:选定值是ComboStyle.DropDownList的首选值,而SelectedText是ComboStyle.DropDown的首选值
-这应该能解决你的问题。我花了一个多星期来解决这个小问题。哈 在我的例子中,我绑定到一个列表,而我应该绑定到一个字符串 我在做什么:
private ObservableCollection<string> _SelectedPartyType;
public ObservableCollection<string> SelectedPartyType { get { return
_SelectedPartyType; } set {
_SelectedPartyType = value; OnPropertyChanged("SelectedPartyType"); } }
确认此问题仍然存在于.NET 4中…最好只依赖
SelectedItem
而不是SelectedValue
我目前在.NET 4.5中遇到了SelectedValue
问题。。。对我来说,SelectedItem
也解决了这个问题。我有一个类似的问题,似乎与你描述的相反,我只使用SelectedItem。这个答案解决了这个问题(SelectedItem
实际上做了这两个家伙似乎都认为SelectedValue
应该做的事),但问题的原因是错误的。问题是SelectedValue
和SelectedItem
做了不同的事情。我通过将UpdateSourceTrigger=PropertyChanged
添加到SelectedValue
绑定中,成功地解决了这个问题。对我来说,我不得不设定这个值,这感觉很可笑。谢谢你,这为我指明了正确的方向。我需要ComboBoxItem的Name属性,因此我设置SelectedValuePath=“Name”,它的工作方式类似于champMode=TwoWay
,必须显式设置UpdateSourceTrigger=PropertyChanged。不要混淆它们的默认值。不总是有效的,例如在绑定类的属性时。当您更改属性值not Updated(未更新)时,请使用其中一个,而不是两个或全部3是的,还有另一个像您这样的穷人。还有Int16
vsint
对我来说是个问题。
UpdateSourceTrigger=PropertyChanged
private ObservableCollection<string> _SelectedPartyType;
public ObservableCollection<string> SelectedPartyType { get { return
_SelectedPartyType; } set {
_SelectedPartyType = value; OnPropertyChanged("SelectedPartyType"); } }
private string _SelectedPartyType;
public string SelectedPartyType { get { return _SelectedPartyType; } set {
_SelectedPartyType = value; OnPropertyChanged("SelectedPartyType"); } }