当我使用导航按钮时,组合框wpf数据不显示
本文关键字:组合 wpf 数据 显示 导航 按钮 | 更新日期: 2023-09-27 18:20:38
我创建了一个非常简单的数据库应用程序,因为我是WPF的新手。最终,我能够在winform中使用导航按钮(而不使用数据绑定)。我遇到了一个问题,我希望是一个非常简单的问题,但我又一次不知道?
问题
当我使用导航按钮时,数据不会显示在组合框中,但文本框似乎没有问题,基本上显示为空。我有一个列表视图,所以当CUD操作时,整个记录都会显示出来。我错过了什么吗?
部分类和showdata方法我已经创建了
public partial class MainWindow : Window
{
SqlConnection mconn;
int MaxRows = 0;
int rno;
DataSet ds;
private void MaxRow()
{
MaxRows = ds.Tables[0].Rows.Count;
}
}
public void ShowData()
{
try
{
//pardon me this looks rather tacty
SqlCommand comm = new SqlCommand("Select * from tblTableB", mconn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
listView1.DataContext = dt.DefaultView;
da = new SqlDataAdapter("select * from tblTableB", mconn);
ds = new DataSet();
da.Fill(ds, "TestDatabaseB");
da.Update(ds, "TestDatabaseB");
this.MaxRow();
txtID.Text = ds.Tables[0].Rows[rno][0].ToString();
txtName.Text = ds.Tables[0].Rows[rno][1].ToString();
cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString(); //data not showing
when navigation buttons are used. CB is showning empty
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
导航方法示例
private void PreRec_Click(object sender, RoutedEventArgs e)
{
if (ds.Tables[0].Rows.Count > 0)
{
if (rno > 0)
{
rno--;
ShowData();
}
}
}
private void NxtRec_Click(object sender, RoutedEventArgs e)
{
if (ds.Tables[0].Rows.Count > 0)
{
if (rno < ds.Tables[0].Rows.Count - 1)
{
rno++;
ShowData();
}
}
}
XAML代码
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="381" Width="406" Loaded="Window_Loaded">
<Grid>
<Grid Height="342" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" Width="384">
<ListView Height="134" HorizontalAlignment="Left" Name="listView1" ItemsSource="{Binding}" VerticalAlignment="Top" Width="384">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
<GridViewColumn Header="Health Details" DisplayMemberBinding="{Binding Path=HealthDetails}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="7,181,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Health Details" Height="28" HorizontalAlignment="Left" Margin="7,215,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,152,0,0" Name="txtID" VerticalAlignment="Top" Width="120" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=ID}" IsReadOnly="True" Background="#26000000"></TextBox>
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,186,0,0" Name="txtName" VerticalAlignment="Top" Width="243" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=Name}" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="102,220,0,0" Name="cBHealthDetails" VerticalAlignment="Top" Width="120" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=HealthDetails}">
<ComboBoxItem Content="Yes" />
<ComboBoxItem Content="No" />
</ComboBox>
<Button Content="New" Height="23" HorizontalAlignment="Left" Margin="25,262,0,0" Name="btnNew" VerticalAlignment="Top" Width="75" Click="btnNew_Click" />
<Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="111,262,0,0" Name="btnInsert" VerticalAlignment="Top" Width="75" Click="btnInsert_Click"></Button>
<Button Content="Update" Height="23" HorizontalAlignment="Left" Margin="197,262,0,0" Name="btnUpdate" VerticalAlignment="Top" Width="75" />
<Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="280,262,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" />
<Button Content="<<" Height="23" HorizontalAlignment="Left" Margin="25,301,0,0" Name="FirstRec" VerticalAlignment="Top" Width="75" Click="FirstRec_Click" />
<Button Content="<" Height="23" HorizontalAlignment="Left" Margin="111,301,0,0" Name="PreRec" VerticalAlignment="Top" Width="75" Click="PreRec_Click" />
<Button Content=">" Height="23" HorizontalAlignment="Right" Margin="0,301,112,0" Name="NxtRec" VerticalAlignment="Top" Width="75" Click="NxtRec_Click" />
<Button Content=">>" Height="23" HorizontalAlignment="Left" Margin="280,301,0,0" Name="LastRec" VerticalAlignment="Top" Width="75" Click="LastRec_Click" />
<Label Content="ID" Height="28" HorizontalAlignment="Left" Margin="11,147,0,0" Name="label3" VerticalAlignment="Top" />
</Grid>
</Grid>
非常感谢
更新1
Windows加载的
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ShowData();
}
void ShowData()
{
try
{
SqlCommand comm = new SqlCommand("Select * from tblTableB", mconn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
listView1.DataContext = dt.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void btnShowAll_Click(object sender, RoutedEventArgs e)
{
da = new SqlDataAdapter("select * from tblTableB", mconn);
ds = new DataSet();
da.Fill(ds, "tblTableB");
da.Update(ds, "tblTableB");
this.MaxRow();
txtID.Text = ds.Tables[0].Rows[rno][0].ToString(); //rno["ID"]
txtName.Text = ds.Tables[0].Rows[rno][1].ToString(); //rno["Name"]
cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString(); //also tried by replacing
//rno["HealthDetails"].ToString();
}
XAML代码
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,152,0,0" Name="txtID" VerticalAlignment="Top" Width="120" IsReadOnly="True" Background="#26000000"></TextBox>
<TextBox Height="23" HorizontalAlignment="Left" Margin="102,186,0,0" Name="txtName" VerticalAlignment="Top" Width="243" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="102,220,0,0" Name="cBHealthDetails" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="Yes"></ComboBoxItem>
<ComboBoxItem Content="No"></ComboBoxItem>
</ComboBox>
所以,要么是wpf组合框上有一个bug,要么是对于组合框属性,我需要做一些我错过的事情。我在房产里做的唯一一件事就是。。。项目>集合和添加的内容一个表示是,另一个表示否
我仍在研究,一旦找到解决方案,我会更新
找到解决方案
wpf过程或架构设计似乎与winform非常不同,尤其是combobox。以下代码将不起作用
cBHealthDetails.Text = ds.Tables[0].Rows[rno][2].ToString();
我还必须使用条件循环来消除重复的值。
所以下面的代码对我在这里找到的combox有效,感谢sohel khalifa。。。
for (int intCount = 0; intCount < ds.Tables[0].Rows.Count; intCount++)
{
var val = ds.Tables[0].Rows[intCount]["Health"].ToString();
//check if it already exists
if (!cbHealth.Items.Contains(val))
{
cbHealth.Items.Add(val);
}
}
我甚至没有触摸xmal文件以使代码正常工作。换句话说,我没有在xaml中编写任何代码。我认为大多数开发人员和/或最终用户更喜欢使用DataGrid。
我看到了一些可以改进此代码的方法。首先,不应该每次单击导航按钮都检索数据。其次,应该不需要设置代码中任何控件的Text
。它们都绑定到CCD_ 3上的CCD_。这意味着,当您更改ListView
的SelectedItem
时,它们的值将自动更新。
您应该将数据加载到DataTable
中一次(我假设您在Loaded
事件处理程序中这样做)。你的导航功能应该看起来像:
private void PreRec_Click(object sender, RoutedEventArgs e)
{
if (ds.Tables[0].Rows.Count > 0)
{
if (rno > 0)
{
rno--;
listView1.SelectedItem = ds.Tables[0].Rows[rno];
}
}
}
从那里开始,您的Binding
将完成所有工作(就像您单击ListView
中的一行时的情况一样)。
您的主要问题是您为ListView
设置了ItemsSource
作为"{Binding}"
。。。这绑定到整个MainWindow.cs
类,而不是数据。为您的数据创建一个属性,转而绑定到该属性,然后您将看到数据。
顺便说一下,ListView
不是ComboBox
。
我在顶部更新了一个解决方案