您可以擴展當前的電網控制,並添加一些自定義的依賴項屬性(例如列和行),並綁定到這些。這將允許您保留MVVM模式。
E.G.
public class MyGridControl : Grid
{
public static readonly DependencyProperty RowsProperty =
DependencyProperty.Register("Rows", typeof(int), typeof(MyGridControl), new PropertyMetadata(RowsChanged));
public static readonly DependencyProperty ColumnsProperty =
DependencyProperty.Register("Columns", typeof(int), typeof(MyGridControl), new PropertyMetadata(ColumnsChanged));
public static void RowsChanged(object sender, DependencyPropertyChangedEventArgs args)
{
((MyGridControl)sender).RowsChanged();
}
public static void ColumnsChanged(object sender, DependencyPropertyChangedEventArgs args)
{
((MyGridControl)sender).ColumnsChanged();
}
public int Rows
{
get { return (int)GetValue(RowsProperty); }
set { SetValue(RowsProperty, value); }
}
public int Columns
{
get { return (int)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public void RowsChanged()
{
//Do stuff with this.Rows
//E.G. Set the Row Definitions and heights
}
public void ColumnsChanged()
{
//Do stuff with this.Columns
//E.G. Set the Column definitions and widths
}
如果你的虛擬機有屬性 '行' 和 '列',在XAML應該是這樣的:
<local:MyGridControl
Rows="{Binding Rows}"
Columns="{Binding Columns}">
</local:MyGridControl>
太好了 - 謝謝! – Peter