我对 wpf 很陌生,所以也许我在这里遗漏了一些明显的东西。我在 stackoverflow 中发现了一个类似的问题,并尝试了解决方案(在下面给出的代码中实现)但无法让它工作。链接在这里。让我解释一下这个问题。我正在使用框架 2010。
我有一个包含 1 个数据网格和 3 个按钮的用户控件。相同的xaml如下。
<UserControl x:Class="RadarControls.RadarDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:local="clr-namespace:RadarControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<DataGrid Name="myGrid" Grid.ColumnSpan="3" ItemsSource="{Binding}" AutoGenerateColumns="True"></DataGrid>
<Button Content="Add" Grid.Row="1" Height="23" Margin="0,2,2,0" HorizontalAlignment="Left" Name="btnAdd" Width="100" />
<Button Content="Delete" Grid.Column="1" Grid.Row="1" Margin="0,2,2,0" Height="23" HorizontalAlignment="Left" Name="btnDelete" Width="100" />
<Button Content="Save" Grid.Column="2" Grid.Row="1" Height="23" Margin="0,2,0,0" HorizontalAlignment="Left" Name="btnSave" Width="100" />
</Grid>
</UserControl>
我正在使用此用户控件的窗口的 xaml 如下
<Window x:Class="RadarStudio.Users"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrls="clr-namespace:RadarControls;assembly=RadarControls"
xmlns:vm='clr-namespace:RadarViewModel.Users;assembly=RadarViewModel'
Title="Users" Height="300" Width="300">
<Grid >
<ctrls:RadarDataGrid Name="grid1" DataContext="{Binding str}"></ctrls:RadarDataGrid>
</Grid>
我后面的窗口代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.Collections.ObjectModel;
namespace RadarStudio
{
/// <summary>
/// Interaction logic for Users.xaml
/// </summary>
public partial class Users : Window
{
public Users()
{
InitializeComponent();
//this.DataContext = this;
str.Add("dhaval");
str.Add("ravinder");
}
public ObservableCollection<string> str = new ObservableCollection<string>();
}
}
我尝试了很多东西,但我无法在我的网格上找到字符串。
请帮忙!提前致谢!
问候,
萨马尔