简体   繁体   English

C#/ WPF如何从文本框读取“绑定”(数据从SQL表获取)?

[英]C#/WPF How can I read “Bindings” from a TextBox (Data gets from a SQL-Table)?

I have a question. 我有个问题。 I search the problem but I didn't find a solution. 我搜索了问题,但没有找到解决方案。 Hope someone can help me. 希望可以有人帮帮我。

I have a "DataGrid" and in this is a "DataGrid.RowDetailsTemplate". 我有一个“ DataGrid”,其中有一个“ DataGrid.RowDetailsTemplate”。 There are "Textbox" they show data from a list binding. 有“文本框”,它们显示列表绑定中的数据。 And I want to change this data when I click on a button, but I cant read the Bindings so I cant change them. 而且我想在单击按钮时更改此数据,但是我无法读取绑定,因此无法更改它们。 Hope someone know what I mean. 希望有人知道我的意思。

WPF code: WPF代码:

    <dxn:NavBarGroup Name="_navBarOverlay" Header="Benutzerübersicht">
            <Grid Style="{StaticResource GridStyleAccordion}">
                <DataGrid x:Name="userDataGrid" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserResizeRows="True" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" AlternatingRowBackground="{DynamicResource WihaGrauB}" ColumnWidth="auto" ColumnHeaderHeight="30"   AutoGenerateColumns="False" Grid.Row="0"   Width="auto" Height="auto">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Benutzer"  Binding="{Binding Name}" MinWidth="150" Width="2*" />
                        <DataGridTextColumn Header="OperatorID" Binding="{Binding OperatorID}" MinWidth="200" Width="3*"/>
                        <DataGridCheckBoxColumn Header="Aktiv" Binding="{Binding Aktiv}" MinWidth="50" Width="*"/>
                    </DataGrid.Columns>
                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <Image DockPanel.Dock="Left" Source="{Binding ImageUrl}" Height="30" Width="25" Margin="10,0,0,0"/>
                                <Grid Margin="10">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="Name: " FontFamily="{DynamicResource WihaFontFamaly}" FontWeight="Bold" Grid.Row="1" Margin="2,2,2,2"/>
                                    <TextBox  x:Name="nameText" Text="{Binding  Path=Name, Mode=TwoWay}" Grid.Column="1" Width="150" TextAlignment="Center" HorizontalAlignment="Left" Grid.Row="1"  Margin="2,2,2,2"  />
                                    <TextBlock Text="OperatorID: " FontFamily="{DynamicResource WihaFontFamaly}" Grid.Row="2" FontWeight="Bold" Margin="2,2,2,2"/>
                                    <TextBox x:Name="operatorText" IsReadOnly="True" Text="{Binding OperatorID}" Width="150" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2" TextAlignment="Center"  Margin="2,2,2,2" />
                                    <TextBlock Text="DeviceID: " FontFamily="{DynamicResource WihaFontFamaly}" FontWeight="Bold" Grid.Row="3" Margin="2,2,2,2"/>
                                    <TextBox Text="{Binding DeviceID}" Grid.Column="3" Width="150" HorizontalAlignment="Left" Grid.Row="3" TextAlignment="Center"  Margin="2,2,2,2"/>
                                    <TextBlock Text="Passwort: " FontFamily="{DynamicResource WihaFontFamaly}" FontWeight="Bold" Grid.Row="4" Margin="2,2,2,2"/>
                                    <TextBox Text="{Binding Passwort}" Grid.Column="4" Width="150" HorizontalAlignment="Left" Grid.Row="4" TextAlignment="Center"   Margin="2,2,2,2"/>
                                    <TextBlock Text="Aktiv:" FontWeight="Bold" FontFamily="{DynamicResource WihaFontFamaly}" Grid.Row="5" Margin="2,2,2,2"/>
                                    <CheckBox Grid.Column="5" Grid.Row="5" IsChecked="{Binding Aktiv}" Margin="2,2,2,2" />
                                    <Button Style="{StaticResource ButtonStyleWIHA}" Click="saveUserPanel_Click" Name="saveUserPanel" Content="Save" Grid.Row="6" />
                                </Grid>
                            </DockPanel>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>
                </DataGrid>
            </Grid>
        </dxn:NavBarGroup>

C# code where the data comes: 数据来自的C#代码:

     public void ShowAllOperator()
    {
        try
        {
            List<User> users = new List<User>();
            // string sql = "SELECT * FROM wiha_Operators";
            SqlConnection conn = new SqlConnection("Server=127.0.0.1;Database=Wiha;Trusted_Connection=true");
            conn.Open();
            // SqlCommand cmd = new SqlCommand(sql, conn);
            SqlCommand cmd = new SqlCommand("dbo.wiha_operators_SelectAll", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {

                while (rdr.Read())
                {
                    users.Add(new User() { Name = rdr.GetValue(2).ToString(), OperatorID = rdr.GetValue(0).ToString(), Aktiv = rdr.GetValue(4).Equals(true), ImageUrl = "C:/Users/Jason/Desktop/DeleteIcon.png", DeviceID = rdr.GetValue(1).ToString(), Passwort = rdr.GetValue(3).ToString() });
                }

            }
            userDataGrid.ItemsSource = users;
        }
        catch (Exception)
        {

            throw;
        }
    }

Here I want to change them: 在这里我要更改它们:

     private void saveUserPanel_Click(object sender, RoutedEventArgs e)
    {


    }

My User list: 我的用户列表:

     public class User
    {
        public string Name { get; set; }
        public string OperatorID { get; set; }
        public bool Aktiv { get; set; }
        public string ImageUrl { get; set; }
        public string DeviceID { get; set; }
        public string Passwort { get; set; }

    }

Hope you know what I want from you. 希望你知道我想要你什么。 You miss or didn't understand something? 你想念还是不明白? Ask me :) 问我 :)

EDIT 编辑

My new User class: 我的新User类:

    public class User : INotifyPropertyChanged
    {
        private string _name = string.Empty;
        public event PropertyChangedEventHandler PropertyChanged;
        public string Name
        {
            get { return this._name; }
            set
            {
                if (value != this._name)
                {
                    this._name = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public string OperatorID { get; set; }
        public bool Aktiv { get; set; }
        public string ImageUrl { get; set; }
        public string DeviceID { get; set; }
        public string Passwort { get; set; }

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

I see only the old value. 我只看到旧值。

As @Ash suggests, you could use the DataContext of the Button, which is a User object, to access the Item, since it is part of the DataTemplate 正如@Ash所建议的,您可以使用作为User对象的Button的DataContext来访问Item,因为它是DataTemplate一部分

private void saveUserPanel_Click(object sender, RoutedEventArgs e)
{
  var user = (sender as Button).DataContext as User;
  if(user != null)
  {
    user.Name = "NewName";
    var active = user.Aktiv;
    // ...
  }
}

Notice that updating the user object in the code behind will not update your UI since User does not implement INotifyPropertyChanged . 请注意,由于User未实现INotifyPropertyChanged ,因此在后面的代码中更新用户对象不会更新您的UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ASP.NET从C#中的SQL Server表中自动完成TextBox中的值 - How can I autocomplete values in a TextBox from SQL Server table in C# with ASP.NET 如何从C#中的TextBox加载SQL数据源? - How can I load SQL data source from a TextBox in C#? 如何在WPF C#中将数据从DataGrid显示到文本框 - How to display data from datagrid to textbox in wpf c# 我如何使用C#将表中的数据添加到WPF中的列表视图中 - How i can add data from table into list view in wpf using C# C# / WPF:如何以编程方式创建所需数量的 texblocks 以显示表中的数据 - C# / WPF : how can I programatically create as many texblocks as needed to show data from a table 如何从SQL Server数据库中的AC文本框中读取1条记录? - How to read 1 record from a sql server database into a c# textbox? 如何从C#的数据表中的给定HTML文档中读取特定数据? - How can I read specific data from given html document in Data Table in c#? 如何从 TextBox_KeyDown 事件中获取文本? WPF C# - How can I get the text from the TextBox_KeyDown event? WPF C# C#/ WPF:如何从ViewModel中找出在TextBox中选择的内容? - C#/WPF: How can I from a ViewModel find out what is Selected in a TextBox? C# WPF:如何从另一个页面访问页面控件以获取 textbox.text - C# WPF : how can i access page controls from another page to get textbox.text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM