English 中文(简体)
WPF DataGrid (C# 4.0)
原标题:Databinding in WPF DataGrid (C# 4.0)

I m 刚刚与世界森林论坛玩,,I m 存在数据约束问题。

我的法典

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>

窗口背后的代码:

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

基本上。 它制定了登记册行动目标清单,并以数据分类。 然而,我发现,每当我管理代谢克岛的法典时,尽管名单已经更新,但其内容却未在格里德得到更新。

简言之,谁能发现我所不知的是什么?

Martin.

最佳回答

页: 1 ObservableCollection:

protected ObservableCollection<RegistryAction> mRegistryActions = new ObservableCollection<RegistryAction>();
问题回答

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签