【发布时间】:2018-09-12 07:24:28
【问题描述】:
我有一个枚举类,它的状态很少。我想将我的枚举绑定到组合框,一旦单击保存按钮,它应该使用 mvvm 模式保存到数据库中。现在,我可以将枚举状态填充到组合框中,但我可以将它绑定到视图模型吗?以及如何从枚举中保存到数据库中。
这里是 xaml 代码:
xmlns:enum="clr-namespace:application.Enum"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="Home" Height="450" Width="700">
<Window.DataContext>
<vm:ProductionLineConfigViewModel/>
</Window.DataContext>
<Window.Resources>
<ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="enum:Status"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<ComboBox x:Name="combobox_status" Grid.Column="2" Grid.Row="3" Margin="5.8,41.8,43.8,0" VerticalAlignment="Top" SelectionChanged="combobox_status_SelectionChanged"
ItemsSource="{Binding Source={StaticResource dataFromEnum}}" SelectedItem="{Binding ProductionLineStatus}" SelectedValue="{Binding ProductionLineStatus, Mode=TwoWay}" SelectedValuePath="ProductionLineStatus"/>
<Button Grid.Column="1" Grid.Row="5" Content="Back" Margin="24.8,7,24.8,42.6" x:Name="btnBack" Click="btnBack_Click"/>
<Button Grid.Column="2" Grid.Row="5" Content="Create" Margin="24.8,7,24.8,42.6" x:Name="btnCreate" Click="btnCreate_Click" Command="{Binding NewProductionLineConfigCommand}"/>
</Grid>
这是我现在收到的错误消息:
System.Windows.Data 错误:40:BindingExpression 路径错误: 在“对象”“状态”上找不到“ProductionLineStatus”属性 (哈希码 = 0)'。绑定表达式:路径=生产线状态; DataItem='状态' (HashCode=0);目标元素是“组合框” (名称='combobox_status');目标属性是“NoTarget”(类型 '对象')
这是视图模式:
public class ProductionLineConfigViewModel : INotifyPropertyChanged
{
Database db = new Database();
MySqlDataReader reader;
MySqlDataAdapter da;
DataTable dt = new DataTable();
private ProductionLineConfig productionlineconfig;
public ProductionLineConfig ProductionLineConfigs
{
get { return productionlineconfig; }
set
{
productionlineconfig = value;
OnPropertyChanged("ProductionLineConfigs");
}
}
// TODO - List all productionline configs; Implement observablecollections
public List<ProductionLineConfig> listAllProductionLineConfigs
{
get
{
var plc = new List<ProductionLineConfig>();
string query;
query = "select * from productionlineconfig";
da = new MySqlDataAdapter(query, db.GetConnection());
da.Fill(dt);
reader = db.QueryCommand(query);
while (reader.Read())
{
plc.Add(new ProductionLineConfig()
{
ProductionLineId = Int32.Parse(reader[0].ToString()),
ProductLineCode = reader[1].ToString(),
ProductionLineName = reader[2].ToString(),
ProductionLineStatus = Convert.ToBoolean(reader[3].ToString()),
ProductionLineCreatedDate = Convert.ToDateTime(reader[4].ToString())
});
}
reader.Close();
return plc;
}
}
// TODO - Create new productionline config;
public void createNewProductionLineConfig()
{
string query;
try
{
query = "Insert into productionlineconfig (PRODUCTION_LINE_CODE, PRODUCTION_LINE_NAME, PRODUCTION_LINE_STATUS) Values ('" + ProductionLineConfigs.ProductLineCode + "' , '" + ProductionLineConfigs.ProductionLineName + "' , '" + ProductionLineConfigs.ProductionLineStatus + "')";
db.QueryCommand(query);
Console.WriteLine("User created successfully");
production_line_config plcWindow = new production_line_config();
plcWindow.Hide();
npi_home npiWindow = new npi_home();
npiWindow.Show();
}
catch (MySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
public NewProductionLineConfigCommand newProductionLineConfigCommand { get; set; }
public ProductionLineConfigViewModel()
{
ProductionLineConfigs = new ProductionLineConfig();
newProductionLineConfigCommand = new NewProductionLineConfigCommand(this);
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这是型号代码:
public class ProductionLineConfig : INotifyPropertyChanged
{
private int id;
public int ProductionLineId
{
get { return id; }
set
{
id = value;
OnPropertyChanged("ProductionLineId");
}
}
private string linecode;
public string ProductLineCode
{
get { return linecode; }
set
{
linecode = value;
OnPropertyChanged("ProductLineCode");
}
}
private string linename;
public string ProductionLineName
{
get { return linename; }
set
{
linename = value;
OnPropertyChanged("ProductionLineName");
}
}
private bool status;
public bool ProductionLineStatus
{
get { return status; }
set
{
status = value;
OnPropertyChanged("ProductionLineStatus");
}
}
private DateTime createddate;
public DateTime ProductionLineCreatedDate
{
get { return createddate; }
set
{
createddate = value;
OnPropertyChanged("ProductionLineCreatedDate");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
【问题讨论】:
-
你能添加你的 ViewModel 类吗?
-
@Alexus 生产视图模型类未针对状态实现。
-
那么 ProductionLineStatus 属性在哪里?
-
@Clemens 对不起,我是 mvvm 的初学者。我在模型类中实现了该属性
-
你想达到什么目的?如果您想通过其 ProductionLineStatus 选择来设置 ProductionLineConfigs 属性,则不能将 ItemsSource 绑定到枚举值。相反,它应该绑定到 ProductionLineConfig 对象的集合。