WPF DataGrid 中的数据绑定 (C# 4.0)

发布于 2024-10-31 10:35:00 字数 3660 浏览 3 评论 0原文

我只是在玩 WPF,并且在数据绑定方面遇到问题...

这是到目前为止我的代码...

Window XAML:

<Window x:Class="FRC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Registry Cleaner - By Martin Milan." MinHeight ="350" Height ="350" MinWidth="525" MaxHeight="700" Width="350" Background="#FFC199AA" >
    <DockPanel Background="#FFD9E1E8" Margin="10">
        <Grid DockPanel.Dock="Top"  >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" >Filepath:</Label>
            <TextBox Grid.Column="1" HorizontalAlignment="Stretch" Name="txtFilePath" VerticalAlignment="Stretch" />
        </Grid>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right" >
            <Button Name="butScan" Content="Scan" MinWidth="75" Margin="0,0,10,5" />
            <Button Name="butDelete" Content="Remove RegKeys" Margin="0,0,5,5" Click="butDelete_Click" />  
        </StackPanel>
        <ScrollViewer Margin="0,0,0,5">
            <DataGrid AutoGenerateColumns="False" Name="dgActions" CanUserAddRows="False" CanUserDeleteRows="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding RegKeyPath, Mode=OneWay}" Header="Registry Key" Width="*"/>
                    <DataGridCheckBoxColumn Binding="{Binding DeletePath, Mode=TwoWay}" Header="Can I delete key?" 
                                            MinWidth="110" Width="110" />
                </DataGrid.Columns>
            </DataGrid>    
        </ScrollViewer>

    </DockPanel>
</Window>

窗口背后的代码:

namespace FRC
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        protected List<RegistryAction> mRegistryActions = new List<RegistryAction>();

        public MainWindow()
        {
            InitializeComponent();
            RegistryAction oRegAction = new RegistryAction();
            oRegAction.DeletePath = true;
            oRegAction.RegKeyPath = "A test value";
            mRegistryActions.Add (oRegAction);
            dgActions.DataContext = mRegistryActions;
            dgActions.ItemsSource = mRegistryActions;

        }

        private void butDelete_Click(object sender, RoutedEventArgs e)
        {
            RegistryAction oRegAction = new RegistryAction();
            oRegAction.DeletePath = true;
            Random rGen = new Random();

            oRegAction.RegKeyPath = "A test " + rGen.Next(100).ToString();
            mRegistryActions.Add(oRegAction);

        }


    }
}

RegistryAction 类:

namespace FRC
{
    public class RegistryAction
    {
        public string RegKeyPath { get; set; }
        public bool DeletePath { get; set; }
        public RegistryAction()
        {
            this.DeletePath = false;
            this.RegKeyPath = "";
        }
    }
}

基本上。它设置一个RegistryAction 对象列表,并将其与DataGrid 绑定。然而,我发现每当我在 butDelete_Click 中运行代码时,尽管列表已更新,但网格上的内容并未更新。

简而言之,有人能发现我错过了什么吗?

马丁.

I'm just having a play with WPF, and I'm having a problem with databinding...

Here's my code so far...

The Window XAML:

<Window x:Class="FRC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Registry Cleaner - By Martin Milan." MinHeight ="350" Height ="350" MinWidth="525" MaxHeight="700" Width="350" Background="#FFC199AA" >
    <DockPanel Background="#FFD9E1E8" Margin="10">
        <Grid DockPanel.Dock="Top"  >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" >Filepath:</Label>
            <TextBox Grid.Column="1" HorizontalAlignment="Stretch" Name="txtFilePath" VerticalAlignment="Stretch" />
        </Grid>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right" >
            <Button Name="butScan" Content="Scan" MinWidth="75" Margin="0,0,10,5" />
            <Button Name="butDelete" Content="Remove RegKeys" Margin="0,0,5,5" Click="butDelete_Click" />  
        </StackPanel>
        <ScrollViewer Margin="0,0,0,5">
            <DataGrid AutoGenerateColumns="False" Name="dgActions" CanUserAddRows="False" CanUserDeleteRows="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding RegKeyPath, Mode=OneWay}" Header="Registry Key" Width="*"/>
                    <DataGridCheckBoxColumn Binding="{Binding DeletePath, Mode=TwoWay}" Header="Can I delete key?" 
                                            MinWidth="110" Width="110" />
                </DataGrid.Columns>
            </DataGrid>    
        </ScrollViewer>

    </DockPanel>
</Window>

The code behind for the window:

namespace FRC
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        protected List<RegistryAction> mRegistryActions = new List<RegistryAction>();

        public MainWindow()
        {
            InitializeComponent();
            RegistryAction oRegAction = new RegistryAction();
            oRegAction.DeletePath = true;
            oRegAction.RegKeyPath = "A test value";
            mRegistryActions.Add (oRegAction);
            dgActions.DataContext = mRegistryActions;
            dgActions.ItemsSource = mRegistryActions;

        }

        private void butDelete_Click(object sender, RoutedEventArgs e)
        {
            RegistryAction oRegAction = new RegistryAction();
            oRegAction.DeletePath = true;
            Random rGen = new Random();

            oRegAction.RegKeyPath = "A test " + rGen.Next(100).ToString();
            mRegistryActions.Add(oRegAction);

        }


    }
}

The RegistryAction class:

namespace FRC
{
    public class RegistryAction
    {
        public string RegKeyPath { get; set; }
        public bool DeletePath { get; set; }
        public RegistryAction()
        {
            this.DeletePath = false;
            this.RegKeyPath = "";
        }
    }
}

Basically. it sets up a list of RegistryAction objects, and binds it with a DataGrid. I am finding however that whenever I run the code in butDelete_Click, although the list is updated, the content isn't getting updated on the Grid.

In short, can anyone spot what I have missed please?

Martin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-07 10:35:00

mRegistryActions 应该是 ObservableCollection

protected ObservableCollection<RegistryAction> mRegistryActions = new ObservableCollection<RegistryAction>();

mRegistryActions should be ObservableCollection:

protected ObservableCollection<RegistryAction> mRegistryActions = new ObservableCollection<RegistryAction>();
蘑菇王子 2024-11-07 10:35:00

您需要让 RegistryAction 实现 INotifyPropertyChanged。 MSDN 还提供了有关此主题的操作方法

You need to have RegistryAction implement INotifyPropertyChanged. MSDN also has a how-to on this subject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文