gpt4 book ai didi

c# - 在wpf中的用户控件内分配DataGrid的itemsSource

转载 作者:行者123 更新时间:2023-11-30 17:58:41 25 4
gpt4 key购买 nike

我对 wpf 很陌生,所以我可能在这里遗漏了一些明显的东西。我在 stackoverflow 中发现了一个类似的问题并尝试了解决方案(在下面给出的代码中实现)但无法让它工作。链接是here .让我解释一下这个问题。我正在使用 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>();
}
}

我尝试了很多东西,但我无法在我的网格上获取字符串。

求助!提前致谢!

问候,

萨马尔

最佳答案

您只能绑定(bind)到公共(public)属性!将 str 作为属性。

来自 MSDN : 用作绑定(bind)的绑定(bind)源属性的属性必须是类的公共(public)属性。不能出于绑定(bind)目的访问显式定义的接口(interface)属性,也不能访问没有基本实现的 protected 、私有(private)、内部或虚拟属性。

您不能绑定(bind)到公共(public)字段。

关于c# - 在wpf中的用户控件内分配DataGrid的itemsSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com