wpfmvvm中的数据绑定
我是WPF和MVVM的新手 我尝试将mysql数据库中的数据提取到一个wpfmvvm中的数据绑定,wpf,mvvm,observablecollection,Wpf,Mvvm,Observablecollection,我是WPF和MVVM的新手 我尝试将mysql数据库中的数据提取到一个observateCollection,然后将其绑定到UI 问题是,我在模型层的第一个ObservableCollection中有数据,但是当我无法将数据发送到ViewModel层时,就不会有任何东西绑定到UI class Databasecon { int i = 0; // First Binding for the Database public ObservableCollection<
observateCollection
,然后将其绑定到UI
问题是,我在模型层的第一个ObservableCollection
中有数据,但是当我无法将数据发送到ViewModel层时,就不会有任何东西绑定到UI
class Databasecon
{
int i = 0;
// First Binding for the Database
public ObservableCollection<Operator> operators { get; private set; }
public Databasecon()
{
this.operators = new ObservableCollection<Operator>();
}
public void Datacon(string conn)
{
MySqlConnection con = null;
MySqlCommand com = null;
MySqlDataReader myreader = null;
int columnOrdinaloperatorname = -1;
con = new MySqlConnection(conn);
try
{
if (com == null)
{
com = new MySqlCommand("SELECT operator_name FROM operators", con);
com.Connection.Open();
myreader = com.ExecuteReader();
columnOrdinaloperatorname = myreader.GetOrdinal("operator_name");
while (myreader.Read())
{
this.operators.Add(new Operator() { operatorname = myreader.GetString(columnOrdinaloperatorname).ToString() });
i++;
}
}
MessageBox.Show(operators.Count.ToString());
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (myreader != null)
myreader.Close();
if (com != null)
{
if (com.Connection != null)
com.Connection.Close();
}
}
}
}
class Operator
{
public string operatorname { get ; set; }
}
class collect : INotifyPropertyChanged
{
private Databasecon databasecon = null;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Operator> operators
{
get
{
if (this.databasecon.operators != null)
{
return this.databasecon.operators;
}
else
{
return null;
}
}
set
{
this.operators = value; RaisePropertyChanged("operators");
}
}
private void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public collect()
{
this.databasecon = new Databasecon();
}
}
classdatabasecon
{
int i=0;
//数据库的第一个绑定
公共可观测集合运算符{get;private set;}
公共数据库
{
this.operators=新的ObservableCollection();
}
公共无效数据控制(字符串控制)
{
MySqlConnection con=null;
MySqlCommand com=null;
MySqlDataReader myreader=null;
int columnOrdinalLoperAtorName=-1;
con=新的MySqlConnection(conn);
尝试
{
如果(com==null)
{
com=新的MySqlCommand(“从操作员中选择操作员名称”,con);
com.Connection.Open();
myreader=com.ExecuteReader();
ColumnOrdinalLoperAtorName=myreader.GetOrdinal(“运算符名称”);
while(myreader.Read())
{
Add(new Operator(){operatorname=myreader.GetString(ColumnOrderLoperAtorName.ToString()});
i++;
}
}
Show(operators.Count.ToString());
}
捕获(MySqlException-ex)
{
Show(例如ToString());
}
最后
{
如果(myreader!=null)
myreader.Close();
如果(com!=null)
{
if(com.Connection!=null)
com.Connection.Close();
}
}
}
}
类运算符
{
公共字符串运算符名称{get;set;}
}
类集合:INotifyPropertyChanged
{
私有Databasecon Databasecon=null;
公共事件属性更改事件处理程序属性更改;
公共可观测收集操作员
{
得到
{
if(this.databasecon.operators!=null)
{
返回this.databasecon.operators;
}
其他的
{
返回null;
}
}
设置
{
this.operators=值;RaisePropertyChanged(“运算符”);
}
}
私有void RaisePropertyChanged(字符串propertyName)
{
var handler=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共收费
{
this.databasecon=新的databasecon();
}
}
Xaml代码的一个示例是:
<Window.DataContext>
<vm:collect/>
</Window.DataContext>
<Grid>
<TextBlock Margin="459,51,-459,-51"><InlineUIContainer>
<TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="{Binding Path=operators}" Width="198" FontSize="28" Height="66"/>
</InlineUIContainer></TextBlock>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="102" Margin="115,240,0,0" VerticalAlignment="Top" Width="339" ItemsSource="{Binding operators}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock >
<Run Text="{Binding operatorname}"></Run>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
有人能帮我吗?我觉得你的绑定很好-下面是我测试它们的步骤:
<Window.DataContext>
<vm:collect/>
</Window.DataContext>
<Grid>
<Button VerticalAlignment="Top" HorizontalAlignment="Left" Command="{Binding AddThings}" Height="25" Width="Auto">Add Stuff</Button>
<TextBlock Margin="459,51,-459,-51"><InlineUIContainer>
<TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="{Binding Path=operators}" Width="198" FontSize="28" Height="66"/>
</InlineUIContainer></TextBlock>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="102" Margin="115,240,0,0" VerticalAlignment="Top" Width="339" ItemsSource="{Binding operators}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock >
<Run Text="{Binding operatorname}"></Run>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
您的绑定对我来说似乎很好-以下是我测试它们所做的:
<Window.DataContext>
<vm:collect/>
</Window.DataContext>
<Grid>
<Button VerticalAlignment="Top" HorizontalAlignment="Left" Command="{Binding AddThings}" Height="25" Width="Auto">Add Stuff</Button>
<TextBlock Margin="459,51,-459,-51"><InlineUIContainer>
<TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="{Binding Path=operators}" Width="198" FontSize="28" Height="66"/>
</InlineUIContainer></TextBlock>
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="102" Margin="115,240,0,0" VerticalAlignment="Top" Width="339" ItemsSource="{Binding operators}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock >
<Run Text="{Binding operatorname}"></Run>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
谢谢你的回答。你能给我解释一下我是如何在UI加载中使用ICommand接口而不是Button进行绑定的吗?我不太清楚你的意思。您的意思是要填充列表,以便在UI加载时准备就绪?在这种情况下,您只需要在collect构造函数中添加对databasecon.Datacon(“[连接字符串]”)的调用。感谢您的帮助,现在可以工作了。这就是我所需要的。谢谢您的回答。您能解释一下我如何在UI加载中使用ICommand接口而不是使用Button进行绑定我不太确定您的意思。您的意思是要填充列表,以便在UI加载时准备就绪?在这种情况下,您只需要在collect构造函数中添加对databasecon.Datacon(“[connection string]”)的调用。感谢您的帮助,现在已经完成了,这就是我所需要的
<Window.DataContext>
<vm:collect x:Name="Collect"/>
</Window.DataContext>
window.Collect.Datacon("[My connection string]");
public MainWindow(object viewModel)
{
DataContext = viewModel;
InitializeComponent();
}