【发布时间】:2012-11-08 18:48:15
【问题描述】:
问题在于 DependencyProperty 未分配给 XAML 中指定的值:
我有两个不同的用户控件,比如说 UserControl1 和 UserControl2,它们定义了两个控件共有的另一个用户控件:
UserControl1.xaml:
<modulename:MyUserControl ....somecode... DataOrientation="Horizontal">
</modulename:MyUserControl>
UserControl2.xaml
<modulename:MyUserControl ....somecode... DataOrientation="Vertical">
</modulename:MyUserControl>
两个 UserControl 的区别在于 UserControl1 必须使用水平方向显示数据,而 UserControl2 必须使用垂直方向显示数据。
在 MyUserControl 的代码中,我定义了一个依赖属性,如下所示:
public static readonly DependencyProperty DataOrientationProperty =
DependencyProperty.Register ("DataOrientation",typeof(String),typeof(MyUserControl));
public String DataOrientation
{
get {return (String)GetValue(DataOrientationProperty);}
set { SetValue(DataOrientationProperty, value); }
}
这些是 MyUserControl.xaml 中的代码片段:
...
<StockPanel>
<Grid Name="MyGrid" SizeChanged="MyGrid_SizeChanged">
<Grid.ColumnDefinitions>
<ColumnDefinitions Width="*"/>
<ColumnDefinitions Width="100"/>
</Grid.ColumnDefinitions>
<ScrollViewer Name="MySV" Grid.Column="0" ....>
<Grid Name="DetailGrid"/>
</ScrollViewer>
<Grid Grid.Column="1" .........>
......Some Option Data....
</Grid>
</Grid>
</StockPanel>
想法是根据方向标志将 ColumnDefinitions 更改为 RowDefinitions 并将 Grid.Column 更改为 Grid.Rows:
如果标志为“Horizontal”,则UserControl并排显示“DetailsGrid”和“Option Data”网格,表示“DetailGrid”在第0列,“Option Data”网格在第1列。
如果标志是“垂直”,用户控件在第 1 行显示“DetailGrid”,在第 0 行显示“选项数据”。
在这个问题上需要一些帮助。
提前谢谢你。
【问题讨论】:
-
我认为您需要一个
DataTrigger,它根据{Binding RelativeSource={RelativeSource Self}, Path=DataOrientation}的值设置对象的Grid.Row和Grid.Column