簡體   English   中英

WPF DataGrid(C#4.0)中的數據綁定

[英]Databinding in WPF DataGrid (C# 4.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中運行代碼時,盡管列表已更新,但內容並未在Grid上更新。

簡而言之,任何人都可以發現我錯過的東西嗎?

馬丁。

mRegistryActions應該是ObservableCollection

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

您需要具有RegistryAction實現INotifyPropertyChanged MSDN也有關於此主題的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM