我需要在程序中更改columnDefinition的width和rowDefinition的Height,所以绑定对我来说似乎是一个不错的选择。 我尝试将ColumnDefinition的width和RowDefinition的Grid高度绑定到一个Object“StrategyObject”属性。 对于演示,我们尝试更改以下网格的列和行的大小(请参阅屏幕截图)。
我将策略模式与抽象类“BaseStrategyObject”:
一起使用public abstract class BaseStrategyObject
{
protected GridLength gridFirstRowHeight;
public GridLength GridFirstRowHeight
{
get { return this.gridFirstRowHeight; }
}
protected GridLength gridSecondRowHeight;
public GridLength GridSecondRowHeight
{
get { return this.gridSecondRowHeight; }
}
protected GridLength gridFirstColumnWidth;
public GridLength GridFirstColumnWidth
{
get { return this.gridFirstColumnWidth; }
}
protected GridLength gridSecondColumnWidth;
public GridLength GridSecondColumnWidth
{
get { return this.gridSecondColumnWidth; }
}
}
以下是三个具体的startegy类:
public class Strategy1Object : BaseStrategyObject
{
public Strategy1Object()
{
this.gridFirstColumnWidth = new GridLength(1, GridUnitType.Star);
this.gridFirstRowHeight = new GridLength(1, GridUnitType.Star);
this.gridSecondColumnWidth = new GridLength(1, GridUnitType.Star);
this.gridSecondRowHeight = new GridLength(1, GridUnitType.Star);
}
}
public class Strategy2Object : BaseStrategyObject
{
public Strategy2Object()
{
this.gridFirstColumnWidth = new GridLength(80, GridUnitType.Star);
this.gridFirstRowHeight = new GridLength(1, GridUnitType.Star);
this.gridSecondColumnWidth = new GridLength(20, GridUnitType.Star);
this.gridSecondRowHeight = new GridLength(1, GridUnitType.Star);
}
}
public class Strategy3Object : BaseStrategyObject
{
public Strategy3Object()
{
this.gridFirstColumnWidth = new GridLength(80.0, GridUnitType.Pixel);
this.gridFirstRowHeight = new GridLength(200.0, GridUnitType.Pixel);
this.gridSecondColumnWidth = new GridLength(200.0, GridUnitType.Pixel);
this.gridSecondRowHeight = new GridLength(80.0, GridUnitType.Pixel);
}
}
窗口的Xaml代码:
<Window x:Class="GridAsteriskBindingtest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Center" Orientation="Horizontal">
<Button Click="Strategy1_Click">Strategy1</Button>
<Button Click="Strategy2_Click">Strategy2</Button>
<Button Click="Strategy3_Click">Strategy3</Button>
</StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding Mode=OneWay, Path=StrategyObject.GridFirstRowHeight}"/>
<RowDefinition Height="{Binding Mode=OneWay, Path=StrategyObject.GridSecondRowHeight}"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Mode=OneWay, Path=StrategyObject.GridFirstColumnWidth}"/>
<ColumnDefinition Width="{Binding Mode=OneWay, Path=StrategyObject.GridFirstColumnWidth}"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0"
Fill="Red"/>
<Rectangle Grid.Row="0" Grid.Column="1"
Fill="Yellow"/>
<Rectangle Grid.Row="1" Grid.Column="0"
Fill="Blue"/>
<Rectangle Grid.Row="1" Grid.Column="1"
Fill="Green"/>
</Grid>
</DockPanel>
和窗口的.cs:
public partial class MainWindow : Window , INotifyPropertyChanged
{
private BaseStrategyObject strategyObject;
public BaseStrategyObject StrategyObject
{
get { return this.strategyObject; }
set
{
this.strategyObject = value;
OnStrategyObjectChanged();
}
}
public MainWindow()
{
this.strategyObject = new Strategy1Object();
InitializeComponent();
}
private void Strategy1_Click(object sender, RoutedEventArgs e)
{
this.StrategyObject = new Strategy1Object();
}
private void Strategy2_Click(object sender, RoutedEventArgs e)
{
this.StrategyObject = new Strategy2Object();
}
private void Strategy3_Click(object sender, RoutedEventArgs e)
{
this.StrategyObject = new Strategy3Object();
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnStrategyObjectChanged()
{
if(this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("StrategyObject"));
}
#endregion
}
单击按钮将新对象应用于“StrategyObject”属性。
当我们启动该程序时,Strategy3(具有特定大小GridUnit.Pixel)工作正常,但它不适用于其他两个策略(策略1和2(未考虑星数: - ())。
以下是ScreenShots(策略1和2给出相同的结果)。
我做错了吗?谢谢!