我在 WPF 中创建了一个带有数据绑定的组合框.我不确定如何获取“comboboxselecteditem"的值和设置值. [英] I have created a combo box with data binding in WPF. I am not sure how to get value and set value of "comboboxselecteditem"
问题描述
这是我的带有数据绑定的组合框的 Xaml 代码:
This my Xaml code for combo box with data binding :
<ComboBox Name="C1" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="126,127,0,0" VerticalAlignment="Top" Width="218" SelectionChanged="C1_SelectionChanged" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding CUS_DESCRIPTION}" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid x:Name="gd" TextElement.Foreground="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5" Grid.Column="0" Text="{Binding CUSTOMER_NAME}"/>
<TextBlock Margin="5" Grid.Column="1" Text="{Binding CUS_DESCRIPTION}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ComboBoxItem.IsSelected" Value="True">
<Setter TargetName="gd" Property="Background" Value="Gray"></Setter>
<Setter TargetName="gd" Property="TextElement.Foreground" Value="White"></Setter>
</Trigger>
<Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
<Setter TargetName="gd" Property="Background" Value="Blue"></Setter>
<Setter TargetName="gd" Property="TextElement.Foreground" Value="White"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
我在 WPF 应用程序中创建了一个带有数据绑定的组合框.组合框项目是通过使用 Observable Collection 对象添加的.我不确定如何获取comboboxselecteditem"的值和设置值.我使用了combobox.selecteditem.tostring();"获取值,但它显示加载组合框时使用的窗口名称+对象名称".同样,我无法将组合框的值设置为默认值.我需要像当窗口打开时组合框selectedboxitem"应该保留我设置的值.
I have created a combo box with data binding in WPF application. Combo box items are add by using Observable Collection object.I am not sure how to get value and set value of "comboboxselecteditem". I used "combobox.selecteditem.tostring();" to get value, but it shows "windows name + object name that used in loading combo box". similarly I am not able to set a value to combo box as a default value.I need like when window opens the combo box "selectedboxitem" should hold the value that I set.
这是我的 C# 代码:
This my c# code:
public partial class MainWindow : Window
{
private ObservableCollection<CUSTOMER> CUSTOMERS=new ObservableCollection<CUSTOMER> ();
public MainWindow()
{
InitializeComponent();
c2.Items.Add("hi");
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\mani\Documents\RAVI.mdb";
try
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from RICE_CUSTOMER";
cmd.Connection = connect;
connect.Open();
cmd.ExecuteNonQuery();
OleDbDataReader R1 = cmd.ExecuteReader();
if (R1.HasRows)
{
while (R1.Read())
{
CUSTOMERS.Add(new CUSTOMER() { CUSTOMER_NAME = R1[0].ToString(), CUS_DESCRIPTION = R1[3].ToString() });
}
DataContext = CUSTOMERS;
}
connect.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window1 W = new Window1();
W.Show();
this.Hide();
}
class CUSTOMER
{
public string CUSTOMER_NAME{get;set;}
public string CUS_DESCRIPTION{get;set;}
}
private void C1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string c=C1.selecteditem.tostring();
Messagebox.show(c);
C1.selecteditem=value;
}
}
}
推荐答案
你可以通过绑定来获取和设置ComboBox
的SelectedItem
,就像你绑定的一样ComboBox
的项目.
You can get and set the ComboBox
's SelectedItem
by binding to it, just as you bound the items of the ComboBox
.
将 UI 的数据成员放入单独的类是一种很好的做法.这通常在使用 MVVM
(模型、视图、视图模型)范例的视图模型中完成.
It is a good practice to put the data members for your UI into a separate class. This is typically done in a view model using the MVVM
(Model, View, View Model) paradigm.
像这样:
class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<CUSTOMER> _customers = new ObservableCollection<CUSTOMER>();
public IList<CUSTOMER> Customers
{
get { return _customers; }
}
private CUSTOMER _selectedCustomer = null;
public CUSTOMER SelectedCustomer
{
get { return _selectedCustomer; }
set
{
_selectedCustomer = value;
OnPropertyChanged("SelectedCustomer");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
在你的窗口类中:
class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
最后在您的 XAML 中:
And finally in your XAML:
<ComboBox ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}">
...
</ComboBox>
我还将从数据库中加载客户的代码移动到 ViewModel
,完成后将 SelectedCustomer
属性设置为合理的默认值.例如,要选择名称为 Bob
的客户,请在从数据库加载对象后运行以下命令:
I would also move the code that loads the customers from the database into the ViewModel
, and once done set the SelectedCustomer
property to a reasonable default. For example, to select the customer whose name is Bob
, run the following once the objects have been loaded from the database:
SelectedCustomer = _customers.FirstOrDefault(customer => customer.CUSTOMER_NAME.Equals("Bob"));
这篇关于我在 WPF 中创建了一个带有数据绑定的组合框.我不确定如何获取“comboboxselecteditem"的值和设置值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!