如何在下面的代码中添加按钮更新按钮和删除按钮来实现以下代码 [英] How to implement the below code in add button update button and delete button in below code

查看:47
本文介绍了如何在下面的代码中添加按钮更新按钮和删除按钮来实现以下代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是Wpf C#中的绑定数据中的ObservableCollection。

请告诉我如何在添加更新删除按钮中实现下面的代码..



点击添加按钮显示datagridview中的所有数据,如(姓名,父姓,DOB,性别,州,地区填写详细信息)。



我尝试过:



xaml code



below code is ObservableCollection in binding data in wpf C#.
so tell me how to implement below code in add update delete button ..

click on add button all data like (name, fathername, DOB, Gender, State, District fill up detail) in show datagridview .

What I have tried:

xaml code

<Window x:Class="Wpf1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        xmlns:local="clr-namespace:Wpf1"
        ResizeMode="NoResize"
        SizeToContent="WidthAndHeight"
        WindowStartupLocation="CenterScreen"
        Height="1000" Width="1100">


  <window.resources>
    <local:EnumBooleanConverter  x:Key="EnumBooleanConverter"/>
  

  <grid>
    <grid.columndefinitions>
      <ColumnDefinition Width="481*" />
      <ColumnDefinition Width="511*" />
    
    <Grid Height="421" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="411">
      <grid.rowdefinitions>
        <RowDefinition Height="50*" />
        <RowDefinition Height="42*" />
        <RowDefinition Height="44*" />
        <RowDefinition Height="43*" />
        <RowDefinition Height="46*" />
        <RowDefinition Height="45*" />
        <RowDefinition Height="45*" />
        <RowDefinition Height="44*" />
        <RowDefinition Height="62*" />
      
      <grid.columndefinitions>
        <ColumnDefinition Width="104*" />
        <ColumnDefinition Width="307*" />
      

      

      <TextBox Height="23" HorizontalAlignment="Left" Margin="3,27,0,0" Name="NameTextBox" VerticalAlignment="Top" Width="211" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Grid.Column="1"/>

      

      <TextBox Height="23" HorizontalAlignment="Left" Margin="2,14,0,0" Name="FathernameTextBox" VerticalAlignment="Top" Width="211" Text="{Binding FatherName, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Column="1" Grid.Row="1"/>

      

      <DatePicker Height="25" HorizontalAlignment="Left" Margin="3,19,0,0" Name="DOBDatePicker" VerticalAlignment="Top" Width="211" Text="{Binding DOB, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Column="1" Grid.Row="2"/>

      

      <RadioButton Content="Male" Margin="3,27,0,0" Name="MaleRadioButton" IsChecked="{Binding Gender, Mode=TwoWay,UpdateSourceTrigger=LostFocus, Converter = {StaticResource EnumBooleanConverter}, ConverterParameter=Male}" Grid.Column="1" HorizontalAlignment="Left" Width="42" Grid.Row="3" />

      <RadioButton Content="Female" Height="16" HorizontalAlignment="Left" Margin="56,27,0,0" Name="FemaleRadioButton" VerticalAlignment="Top" IsChecked = "{Binding Gender, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Female}" Grid.Column="1" Grid.Row="3" />

      

      <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,23,93,0" Name="StateComboBox" VerticalAlignment="Top" Width="211" Text="{Binding State, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Row="4" Grid.Column="1">
        <ComboBoxItem Content="Gujarat"/>
        <ComboBoxItem Content="Rajesthan"/>
        <ComboBoxItem Content="dilhi"/>
      

      

      <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,22,93,0" Name="DistrictComboBox" VerticalAlignment="Top" Width="211" Text="{Binding District, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Grid.Row="5" Grid.Column="1">
        <ComboBoxItem Content="X"/>
        <ComboBoxItem Content="Y"/>
        <ComboBoxItem Content="Z"/>
      

      

      

      

    
    <DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="34,123,0,0" Name ="PersonDatagrid" VerticalAlignment="Top" Width="502"  Grid.Column="1">

      <datagrid.columns>
        <DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Fathername" Width="100" Binding="{Binding FatherName}"/>
        <DataGridTextColumn Header="DOB" Width="70" Binding="{Binding DOB, StringFormat=dd/MM/yyyy}"/>
        <DataGridTextColumn Header="Gender" Width="50" Binding="{Binding Gender, Converter={StaticResource EnumBooleanConverter}}"/>
        <DataGridTextColumn Header="State" Width="80" Binding="{Binding State}"/>
        <DataGridTextColumn Header="District" Width="98" Binding="{Binding District}"/>

      
    
  
</Window

xaml.cs

 public class personalinfo
    {
        public string Name { get; set; }
        public Genders Gender { get; set; }

        public string State { get; set; }

        public string District { get; set; }

        public string DOB { get; set; }

        public string Fathername { get; set; }
    }

   public enum Genders
   {
       Male = 1,
       Female
   }
}

 public  class EnumBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false)
                return DependencyProperty.UnsetValue;

            object paramvalue = Enum.Parse(value.GetType(), ParameterString);
            return paramvalue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ParameterString = parameter as string;
            if (ParameterString == null)
                return DependencyProperty.UnsetValue;

            return Enum.Parse(targetType, ParameterString);
        }

    }

}


namespace Wpf1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            PersonDatagrid.ItemsSource = new List<personalinfo> 
            
            { 
                new personalinfo {Name = "Mariya", Fathername = "shakti", DOB = "26/12/1992", Gender = Genders.Male, State = "Gujarat", District="Baroda" }  
            };
        }
            private void SaveButton_Click(object sender, RoutedEventArgs e)
            {
                //personalinfo.Add(new Button() {Content = $"Button {index++}"});
                Console.WriteLine(string.Format("You clicked on the {0}. button.", (sender as Button).Tag));
             }

            private void DeleteButton_Click(object sender, RoutedEventArgs e)
            {

            }

            private void RefreshButton_Click(object sender, RoutedEventArgs e)
            {

            } 
    }
}

推荐答案

Button {index ++}});
Console.WriteLine(string.Format(你点击了{0}。按钮。,(发件人为Button).Tag));
}

private void DeleteButton_Click(object sender,RoutedEventArgs e)
{

}

private void RefreshButton_Click(object发件人,RoutedEventArgs e)
{

}
}
}
"Button {index++}"}); Console.WriteLine(string.Format("You clicked on the {0}. button.", (sender as Button).Tag)); } private void DeleteButton_Click(object sender, RoutedEventArgs e) { } private void RefreshButton_Click(object sender, RoutedEventArgs e) { } } }


如果你正确的话构建您的viewmodel并将其绑定到控件,您所要做的就是修改viewmodel,当控件检测到绑定列表中的项目发生更改时,控件将自动更新。我希望你的导师在分配这个作业之前和你一起讨论这个问题。



如果不是功课,你应该考虑一个新的职业。
If you properly construct your viewmodel and bind it to the control, all you have to do is modify the viewmodel, and the control will automagically update itself when it detects a change to the items in the bound list. I'm hoping your instructor went over this with you before assigning this homework.

If it is not indeed homework, you should consider a new career.


我没有看到你的.cs代码中使用的 ObservableCollection

见这里的例子:如何:创建并绑定到ObservableCollection [ ^ ]

在这里: Mitesh Sureja的博客:将ObservableCollection绑定到WPF中的ListBox [< a href =http://miteshsureja.blogspot.nl/2011/07/binding-observablecollection-to-listbox.html\"target =_ blanktitle =New Window> ^ ]
I don't see ObservableCollection being used in your .cs code.
See example here: How to: Create and Bind to an ObservableCollection[^]
And here: Mitesh Sureja's Blog: Binding ObservableCollection to ListBox in WPF[^]


这篇关于如何在下面的代码中添加按钮更新按钮和删除按钮来实现以下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆