我打算與兩個評論者同意,綁定是的方式做到這一點,我知道它看起來起初非常複雜,但一旦你轉換的一個很好的圖書館和命令一起它可以一次又一次地重複使用,上面的一個簡單的數據綁定例子看起來像這樣。
XAML,創建一個新項目,並在主窗口粘貼這段代碼,它所做的只是添加一個數據網格,其中包含3列和一個將添加新行的按鈕。請注意,此窗口的數據上下文被設置爲自身,這意味着默認情況下,類MainWindow上的屬性將使用{Binding}公開。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="432,289,0,0" VerticalAlignment="Top" Width="75" Command="{Binding AddCommand}"/>
<DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="274" Width="497" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Field1}"/>
<DataGridTextColumn Binding="{Binding Field2}"/>
<DataGridTextColumn Binding="{Binding Field3}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
現在後面的代碼主窗口,在這裏我已經創建了一個我們可以綁定2個屬性,一個是它包含的會出現什麼作爲行,另一種是可以被稱爲命令的數組項添加另一行。
這裏的ObservableCollection有方法告訴綁定引擎何時更改行,所以你不必費心更新自己的網格。
public partial class MainWindow : Window
{
public ObservableCollection<Item> Items
{
get { return (ObservableCollection<Item>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<Item>), typeof(MainWindow), new PropertyMetadata(null));
public SimpleCommand AddCommand
{
get { return (SimpleCommand)GetValue(AddCommandProperty); }
set { SetValue(AddCommandProperty, value); }
}
public static readonly DependencyProperty AddCommandProperty = DependencyProperty.Register("AddCommand", typeof(SimpleCommand), typeof(MainWindow), new PropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
Items = new ObservableCollection<Item>();
AddCommand = new SimpleCommand(para =>
{
string nextNumber = Items.Count.ToString();
Items.Add(new Item() { Field1 = nextNumber, Field2 = nextNumber, Field3 = nextNumber });
});
}
}
Item類,這只是一個非常簡單的類,它有符合您的數據網格,字段1這3個屬性,2 & 3這裏的依賴屬性也意味着你不必費心更新網格當數據改變時你自己。
public class Item : DependencyObject
{
public string Field1
{
get { return (string)GetValue(Field1Property); }
set { SetValue(Field1Property, value); }
}
public static readonly DependencyProperty Field1Property = DependencyProperty.Register("Field1", typeof(string), typeof(Item), new PropertyMetadata(null));
public string Field2
{
get { return (string)GetValue(Field2Property); }
set { SetValue(Field2Property, value); }
}
public static readonly DependencyProperty Field2Property = DependencyProperty.Register("Field2", typeof(string), typeof(Item), new PropertyMetadata(null));
public string Field3
{
get { return (string)GetValue(Field3Property); }
set { SetValue(Field3Property, value); }
}
public static readonly DependencyProperty Field3Property = DependencyProperty.Register("Field3", typeof(string), typeof(Item), new PropertyMetadata(null));
}
最後的命令類,這是可以一遍又一遍地重複使用在所有項目的鏈接的東西后面的代碼。
public class SimpleCommand : DependencyObject, ICommand
{
readonly Action<object> _execute;
readonly Func<object, bool> _canExecute;
public event EventHandler CanExecuteChanged;
public SimpleCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
_canExecute = canExecute == null ? parmeter => { return true; } : canExecute;
_execute = execute;
}
public virtual void Execute(object parameter)
{
_execute(parameter);
}
public virtual bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
}
那麼恕我直言,'DataBinding'是WPF和SL最簡單的方法。^^ – DHN 2013-03-15 08:10:23
是數據綁定將是做到這一點的最簡單方法。否則,每次更改數據時,都必須編寫自己的邏輯來更新數據網格。對於數據綁定,這意味着您將有一個綁定到datagrid的itemssource屬性的集合。您可能可以非常輕鬆地在集合上調用ToArray()或ToList()方法來提取結果。 – failedprogramming 2013-03-15 08:16:09