【问题标题】:How to create usercontrol using Gridview and Textbox in wpf如何在 wpf 中使用 Gridview 和 Textbox 创建用户控件
【发布时间】:2014-07-23 04:41:26
【问题描述】:

我想在 WPF 中使用 gridview 和文本框创建一个用户控件。

我想在网格视图中选择一行时在文本框上显示数据。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 你觉得它有用吗?

标签: wpf wpf-controls wpfdatagrid


【解决方案1】:

请找到以下解决方案:

用户控件 XAML 代码

<UserControl x:Class="WpfApplication1.DataGridControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="15*"></RowDefinition>
            <RowDefinition Height="85*"></RowDefinition>

        </Grid.RowDefinitions>  
        <TextBox x:Name="txtvalue" Width="100" Grid.Row="0" />
        <DataGrid x:Name="dg" SelectionChanged="dg_SelectionChanged" ItemsSource="{Binding Path=.}" Grid.Row="1" IsReadOnly="True" />
    </Grid>
</UserControl>

用户控件背后的代码

using System;
using System.Data;
using System.Windows.Controls;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for DataGridControl.xaml
    /// </summary>
    public partial class DataGridControl : UserControl
    {
        public DataGridControl()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
            dt.Columns.Add("Column1");
            dt.Columns.Add("Column2");

            dt.Rows.Add("Ashok", "Rathod");
            dt.Rows.Add("Paresh", "Jadav");
            dt.Rows.Add("Keyur", "Kamdar");

            dt.AcceptChanges();

            dg.DataContext = dt;
        }

        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView obj = dg.SelectedItem as DataRowView;
            if(obj != null)
            { 
                txtvalue.Text=Convert.ToString(obj.Row["Column1"]);
            }
        }
    }
}

用于用户控件的MainWindow Xaml

<Window x:Class="WpfApplication1.UserControlMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:uc="clr-namespace:WpfApplication1"
        Title="UserControlMainWindow" Height="300" Width="300">
    <Grid>
        <uc:DataGridControl />
    </Grid>
</Window>

【讨论】:

  • 但是,如何创建动态ItemsSource。我的意思是,“
  • 您可以将 itemsource 更改为任何集合源,因此您必须根据源更改您的 selectionchanged 代码。
  • 但是如何在这个用户控件中创建 ItemsSource 属性呢?谢谢:D
【解决方案2】:

这是一个非常基本的需求描述。您可以尝试创建一个用户控件并添加一个 gridview 控件和一个直接向前的文本框。 然后使用 gridview 的 SelectedIndexChange 事件来选择并显示所选行中的值。尝试此操作后,如果失败,请发布代码。

【讨论】:

  • 我不确定您为什么接受此作为答案,而 Ashok 的以下回答似乎对您有直接帮助。我只是指导你,没有给出任何代码:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多