如何解决如何从另一个用户控件的调用方法绑定datagrid
我创建了两个UserControl
和test1UC.xaml
。
在我的test1UC.xaml中:
test2UC.xaml
在我的test1UC.xaml.cs中:
<Grid>
<Button x:Name="btnTest" Width="100" Foreground="Red" Content="Test Click" Click="btnTest_Click"></Button>
<local:test2UC x:Name="testPWindowLocal" Visibility="Collapsed"></local:test2UC >
</Grid>
但是上述调用函数未将任何数据绑定到datagrid中。始终显示空行。
在我的test1UC.xaml中:
private void btnTest_Click(object sender,RoutedEventArgs e)
{
testPWindowLocal.Visibility = Visibility.Visible;
test2UC testObj = new test2UC();
testObj.loadDataGridTest();
}
在我的test1UC.xaml中:
<Grid>
<DataGrid x:Name="testPSSDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding myData}">
<DataGrid.Columns>
<DataGridTextColumn Header="Customer" Binding="{Binding Customer}"></DataGridTextColumn>
<DataGridTextColumn Header="PID Number" Binding="{Binding PID_Number}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
当我单击using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestDataGridBind
{
/// <summary>
/// Interaction logic for testPSSMainWindowUC.xaml
/// </summary>
public partial class testPSSMainWindowUC : UserControl
{
public testPSSMainWindowUC()
{
InitializeComponent();
//loadDataGridTest();
}
public void loadDataGridTest()
{
DataTable Listtodt = new DataTable();
Listtodt.Columns.Add("Customer");
Listtodt.Columns.Add("PID_Number");
Listtodt.Rows.Add("Test1","test2");
Listtodt.Rows.Add("Test1","test2");
testPSSDataGrid.DataContext = Listtodt.DefaultView;
}
}
}
中的“测试单击”按钮时。我要呼叫Test1UC.xaml
并绑定loadDataGridTest
。
解决方法
您的点击事件处理程序每次都会创建一个新的本地testObj
用户控件。而是访问XAML中定义的现有testPWindowLocal
并调用其loadDataGridTest
方法。
private void btnTest_Click(object sender,RoutedEventArgs e)
{
testPWindowLocal.Visibility = Visibility.Visible;
// Populate the existing user control instead of creating a new one
testPWindowLocal.loadDataGridTest();
}
将XAML绑定到XAML中的myData
不起作用,因为在代码隐藏中有属性myData
,并且您将DataContext
绑定到数据表的默认视图,也没有这样的属性。修改绑定以直接绑定到数据上下文。
<DataGrid x:Name="testPSSDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}">
或者,您可以在test2UC
中直接分配ItemsSource
中的DataGrid
。
public void loadDataGridTest()
{
DataTable Listtodt = new DataTable();
Listtodt.Columns.Add("Customer");
Listtodt.Columns.Add("PID_Number");
Listtodt.Rows.Add("Test1","test2");
Listtodt.Rows.Add("Test1","test2");
// Set the items source of the data grid
testPSSDataGrid.ItemsSource = Listtodt.DefaultView;
}
,
您正在创建两个Test1UC
的控件实例:一个在XAML中,一个在C#中。由于XAML实例的名称为(testPWindowLocal
,因此您可以在隐藏的代码中轻松访问它:
private void btnTest_Click(object sender,RoutedEventArgs e)
{
testPWindowLocal.Visibility = Visibility.Visible;
this.testPWindowLocal.loadDataGridTest();
}
也将Binding
固定在DataGrid
上:
<DataGrid x:Name="testPSSDataGrid" ItemsSource="{Binding}">
或者直接分配DataGrid.ItemsSource
:
testPSSDataGrid.ItemsSource = Listtodt.DefaultView;
应该首选数据绑定。您应该创建一个依赖项属性Listtodt
并将DataGrid.ItemsSource
绑定到该数据源。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。