gpt4 book ai didi

c# - WPF MVVM 组合框 SelectionChanged/SelectedItem

转载 作者:行者123 更新时间:2023-11-29 18:11:18 31 4
gpt4 key购买 nike

我正在使用 MVVM 和 mySql 数据库构建 WPF 应用程序。我很难从存储在属性中的 ComboBox 获取 SelectedItem 。基本上,此时,我希望将 SelectedItem 显示在文本框中。这将帮助我更好地理解绑定(bind)过程,因为我仍在学习。

最终,我想使用存储的值/属性作为从数据库中提取另一个值的引用。下面是一些示例代码。任何帮助将不胜感激。谢谢!

查看

        <Grid Background="AliceBlue">

<Label Content="Street Address" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" Width="87" Height="28"/>
<Label Content="State" HorizontalAlignment="Left" Margin="10,125,0,0" VerticalAlignment="Top" Width="87" Height="22"/>


<TextBox Name="txtStreetAddress" HorizontalAlignment="Left" Height="23" Margin="119,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
Text="{Binding StreetAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
<ComboBox Name="cboState"
HorizontalAlignment="Left" Margin="119,125,0,0" VerticalAlignment="Top" Width="52"
DisplayMemberPath="StateAbb"
ItemsSource="{Binding StateAbbList}"
SelectedItem="{Binding SelectedStateAbb, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>

<TextBox HorizontalAlignment="Left" Height="23" Margin="180,124,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="59"
Text="{Binding SelectedStateAbb, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>


</Grid>

查看模型

public class AddStreetAddressVM : ObservableObject, IPageViewModel
{

public string Name
{
get
{
return "Add Street Address";
}
}

const string dbConnectionString = @"datasource=localhost;port=3306;Initial Catalog='optest1a1';username=root;password=";

private ICommand _cboStateAbb;

private string _streetAddress;
private ObservableCollection<tblStateAbb> _stateAbbList;
private string _selectedStateAbb;
private int _selectedStateNum;

public ICommand CboStateAbb
{
get
{
if (_cboStateAbb == null)
{
_cboStateAbb = new RelayCommand(param => this.fillStateAbb(), null);
}

return _cboStateAbb;
}
}

public string StreetAddress
{
get { return _streetAddress; }
set { SetProperty(ref _streetAddress, value, () => StreetAddress); }
}

public ObservableCollection<tblStateAbb> StateAbbList
{
get { return _stateAbbList; }
set
{
SetProperty(ref _stateAbbList, value, () => StateAbbList);
}
}

public string SelectedStateAbb
{
get { return _selectedStateAbb; }
set
{
SetProperty(ref _selectedStateAbb, value, () => SelectedStateAbb);
//if (_selectedStateAbb != null)
//{
// GetStateNum();
//}

//_selectedStateAbb = value;
}
}

public int SelectedStateNum
{
get { return _selectedStateNum; }
set { SetProperty(ref _selectedStateNum, value, () => SelectedStateNum); }
}

public AddStreetAddressVM() : base()
{
StateAbbList = new ObservableCollection<tblStateAbb>();
fillStateAbb();
}

private void fillStateAbb()
{
using (MySqlConnection con = new MySqlConnection(dbConnectionString))
{
StateAbbList = new ObservableCollection<tblStateAbb>();
con.Open();
string Query = "SELECT * FROM tbl_states";
MySqlCommand createCommand = new MySqlCommand(Query, con);
MySqlDataReader dr = createCommand.ExecuteReader();
int count = 1;
while (dr.Read())
{
string StateAbb = dr.GetString(2);
tblStateAbb stateabb = new tblStateAbb(count, StateAbb);
StateAbbList.Add(stateabb);
count++;
}
con.Close();
}
}

private void GetStateNum()
{
using (MySqlConnection con = new MySqlConnection(dbConnectionString))
{
con.Open();
string Query = "SELECT State_Num FROM tbl_states WHERE State_Abb='" + SelectedStateAbb + "' ";
MySqlCommand createCommand = new MySqlCommand(Query, con);
MySqlDataReader dr = createCommand.ExecuteReader();
int count = 1;
while (dr.Read())
{
int StateNum = dr.GetInt32(1);
StateNum = SelectedStateNum;
}
con.Close();
}

}

}

模型 - 状态缩写

public class tblStateAbb : ObservableObject
{
private Int32 _count;
private String _stateAbb;
private Int32 _stateNum;
private ObservableCollection<tblStateAbb> _tblStateAbb;

public Int32 Count
{
get { return _count; }
set { SetProperty(ref _count, value, () => Count); }
}

public String StateAbb
{
get { return _stateAbb; }
set { SetProperty(ref _stateAbb, value, () => StateAbb); }
}

public Int32 StateNum
{
get { return _stateNum; }
set { SetProperty(ref _stateNum, value, () => StateNum); }
}

public ObservableCollection<tblStateAbb> StateAbbList
{
get { return _tblStateAbb; }
set { SetProperty(ref _tblStateAbb, value, () => StateAbbList); }
}

public tblStateAbb() : base()
{
Count = 0;
StateAbb = "";
StateAbbList = new ObservableCollection<tblStateAbb>();
}

public tblStateAbb(int count, string stateabb) : base()
{
Count = count;
StateAbb = stateabb;
StateAbbList = new ObservableCollection<tblStateAbb>();
}

public tblStateAbb(int count, string stateabb, int statenum) : base()
{
Count = count;
StateAbb = stateabb;
StateNum = statenum;
StateAbbList = new ObservableCollection<tblStateAbb>();
}

}

可观察对象(INotifyPropertyChange)

public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propName)
{
Debug.Assert(GetType().GetProperty(propName) != null);

var pc = PropertyChanged;
if (pc != null)
{
pc(this, new PropertyChangedEventArgs(propName));
}
}

protected bool SetProperty<T>(ref T field, T value, string propName)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
OnPropertyChanged(propName);
return true;
}

return false;
}

protected bool SetProperty<T>(ref T field, T value, Expression<Func<T>> expr)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
var lambda = (LambdaExpression)expr;
MemberExpression memberExpr;

if (lambda.Body is UnaryExpression)
{
var unaryExpr = (UnaryExpression)lambda.Body;
memberExpr = (MemberExpression)unaryExpr.Operand;
}
else
{
memberExpr = (MemberExpression)lambda.Body;
}

OnPropertyChanged(memberExpr.Member.Name);
return true;
}

return false;
}
}

中继命令

    public class RelayCommand : ICommand
{
private Action<object> execute;
private Func<object, bool> canExecute;

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}

public void Execute(object parameter)
{
this.execute(parameter);
}
}

最佳答案

由于SelectedStateAbb是一个字符串,因此您应该将ComboBoxSelectedValuePath属性设置为“StateAbb”并将 SelectedValue 属性绑定(bind)到 SelectedStateAbb:

<ComboBox Name="cboState" 
HorizontalAlignment="Left" Margin="119,125,0,0" VerticalAlignment="Top" Width="52"
DisplayMemberPath="StateAbb"
SelectedValuePath="StateAbb"
ItemsSource="{Binding StateAbbList}"
SelectedValue="{Binding SelectedStateAbb, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

<TextBox HorizontalAlignment="Left" Height="23" Margin="180,124,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="59"
Text="{Binding SelectedStateAbb, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

关于c# - WPF MVVM 组合框 SelectionChanged/SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381176/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com