C#/ WPF如何从TextBox读取“绑定”(数据来自SQL表)?

时间:2017-12-05 15:50:16

标签: c# sql wpf data-binding datagrid

我有一个问题。我搜索问题,但我找不到解决方案。 希望可以有人帮帮我。

我有一个“DataGrid”,这是一个“DataGrid.RowDetailsTemplate”。有“文本框”,它们显示列表绑定中的数据。当我点击按钮时我想更改这些数据,但是我无法阅读Bindings所以我无法更改它们。希望有人知道我的意思。

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#代码:

     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;
        }
    }

我想在这里改变它们:

     private void saveUserPanel_Click(object sender, RoutedEventArgs e)
    {


    }

我的用户列表:

     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; }

    }

希望你知道我对你的要求。你错过了什么或者什么不明白?问我:)

修改

我的新用户类:

    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));
        }
    }

我只看到旧值。

1 个答案:

答案 0 :(得分:1)

正如@Ash建议的那样,你可以使用Button的var plugin = require('plugin.js')来访问Item,因为它是DataContext

User

请注意,更新后面代码中的用户对象不会更新您的用户界面,因为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; // ... } }