C# 如何设置XAML组合框的选定值?
为什么在下面的示例中,组合框设置为空白而不是“先生”? XAML:C# 如何设置XAML组合框的选定值?,c#,xaml,combobox,C#,Xaml,Combobox,为什么在下面的示例中,组合框设置为空白而不是“先生”? XAML: <Window x:Class="TestComb82822.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"
<Window x:Class="TestComb82822.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">
<StackPanel HorizontalAlignment="Left">
<ComboBox SelectedValue="{Binding Salutation}" Width="200">
<ComboBoxItem>Company</ComboBoxItem>
<ComboBoxItem>Ms.</ComboBoxItem>
<ComboBoxItem>Mr.</ComboBoxItem>
</ComboBox>
</StackPanel>
</Window>
using System.Windows;
using System.ComponentModel;
namespace TestComb82822
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Salutation
private string _salutation;
public string Salutation
{
get
{
return _salutation;
}
set
{
_salutation = value;
OnPropertyChanged("Salutation");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Salutation = "Mr.";
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
<Window x:Class="TestCombo234.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">
<StackPanel HorizontalAlignment="Left">
<ComboBox SelectedItem="{Binding Salutation}" Width="200">
<ComboBoxItem>Company</ComboBoxItem>
<ComboBoxItem>Ms.</ComboBoxItem>
<ComboBoxItem>Mr.</ComboBoxItem>
</ComboBox>
</StackPanel>
</Window>
using System.Windows;
using System.ComponentModel;
namespace TestCombo234
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Salutation
private string _salutation;
public string Salutation
{
get
{
return _salutation;
}
set
{
_salutation = value;
OnPropertyChanged("Salutation");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
Salutation = "Mr.";
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
第二次尝试:
Bryan,SelectedItem和WindowLoaded也不起作用,这仍然会使组合框为空:
XAML:
<Window x:Class="TestComb82822.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">
<StackPanel HorizontalAlignment="Left">
<ComboBox SelectedValue="{Binding Salutation}" Width="200">
<ComboBoxItem>Company</ComboBoxItem>
<ComboBoxItem>Ms.</ComboBoxItem>
<ComboBoxItem>Mr.</ComboBoxItem>
</ComboBox>
</StackPanel>
</Window>
using System.Windows;
using System.ComponentModel;
namespace TestComb82822
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Salutation
private string _salutation;
public string Salutation
{
get
{
return _salutation;
}
set
{
_salutation = value;
OnPropertyChanged("Salutation");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Salutation = "Mr.";
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
<Window x:Class="TestCombo234.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">
<StackPanel HorizontalAlignment="Left">
<ComboBox SelectedItem="{Binding Salutation}" Width="200">
<ComboBoxItem>Company</ComboBoxItem>
<ComboBoxItem>Ms.</ComboBoxItem>
<ComboBoxItem>Mr.</ComboBoxItem>
</ComboBox>
</StackPanel>
</Window>
using System.Windows;
using System.ComponentModel;
namespace TestCombo234
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Salutation
private string _salutation;
public string Salutation
{
get
{
return _salutation;
}
set
{
_salutation = value;
OnPropertyChanged("Salutation");
}
}
#endregion
public Window1()
{
InitializeComponent();
DataContext = this;
Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
Salutation = "Mr.";
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
默认情况下,
组合框
确实应该选择它的第一项。但是,您在这里创建的是SelectedValue
的绑定,默认情况下是双向的。更重要的是,在加载时,saltation
(您绑定到的)的值实际上仍然为空。尝试设置称呼=“先生”代码>在初始化组件之前,一切都应该正常。首先需要设置SelectedItem而不是SelectedValue。其次,如果您在实际设置组合框之前设置SelectedItem,请尝试在窗口的loaded事件中设置SelectedItem。在这种情况下,我的解决方案是只使用ItemIndex,即“Mr.”=2您确实在代码中创建了列表/可观察集合
public ObservableCollection<string> Collection
{
get; set;
}
即使在初始化组件之前有saltation=“Mr.”,组合框仍然为空:-(@Edward:也许尝试绑定SelectedItem
而不是XAML中的SelectedValue
?嗯……我能说什么呢?根本没有理由不起作用-我已经做过很多次了-也许其他地方有一些代码做了奇怪的事情。无论如何,试着改变绑定模式
,只是玩玩而已。我使用SelectedIndex最终解决了这个问题,但奇怪的是SelectedItem和SelectedValue在这种情况下不起作用。我试过了,但组合框仍然是空的(上面的代码).Oh!您的项目不是字符串。您需要使用ItemsSource属性来使用SelectedItem和/或SelectedValue。您也可以使用Mr,而不仅仅是Mr。