【问题标题】:Databinding in WPF DataGrid (C# 4.0)WPF DataGrid 中的数据绑定 (C# 4.0)
【发布时间】:2011-04-11 11:21:17
【问题描述】:

我只是在玩 WPF,我遇到了数据绑定问题...

这是我目前的代码...

窗口 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 中运行代码时,虽然列表已更新,但网格上的内容并未更新。

简而言之,谁能发现我错过了什么?

马丁。

【问题讨论】:

    标签: c# wpf data-binding wpfdatagrid


    【解决方案1】:

    mRegistryActions 应该是ObservableCollection

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

    【讨论】:

    • 一举将问题彻底解决了——干杯。应该想到,如果网格没有听到集合中的变化,那么也许集合并没有大喊大叫。 +1 并接受。
    【解决方案2】:

    您需要让RegistryAction 实现INotifyPropertyChanged。 MSDN 还有一个how-to on this subject

    【讨论】:

    • +1 - 干杯...... INotifyCollectionCHange 也需要在 List 上 - 当然我总是可以子类......
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2016-10-07
    • 2011-05-06
    • 2012-08-17
    • 2011-06-15
    • 2020-09-27
    • 2014-10-13
    相关资源
    最近更新 更多