【发布时间】:2015-11-08 06:57:47
【问题描述】:
我正在借助 Visual Studio 2015 在 Windows 10 中使用 https://slideview.codeplex.com 创建一个 Windows 8.1 应用程序。
我在设计中添加了 1 行和 2 列的网格。在第一页有大图像,没有文字,在其他页面有图标和文字。因此,我将 if 4* 放入第一页的第一列,将 2* 放入第二页的第一列,一切都很好,但我想在 ContentPresenter 中使其动态化,然后我可以从 C# 分配它。
请有人帮助我。
我尝试了不同的方式,就像我在 SlideApplicationFrame.cs 中放入下面的代码一样
#region FirstColumnWidth (DependencyProperty)
/// <summary>
/// header Image First Column Width
/// </summary>
public GridLength FirstColumnWidth
{
get { return (GridLength)GetValue(FirstColumnWidthProperty); }
set { SetValue(FirstColumnWidthProperty, value); }
}
public static readonly DependencyProperty FirstColumnWidthProperty =
DependencyProperty.Register("FirstColumnWidth", typeof(GridLength), typeof(SlideApplicationFrame),
new PropertyMetadata(new GridLength(4, GridUnitType.Star)));
#endregion
#region ContentFirstColumnWidth (Attached DependencyProperty)
public static readonly DependencyProperty ContentFirstColumnWidth =
DependencyProperty.RegisterAttached("ContentFirstColumnWidth", typeof(GridLength), typeof(SlideApplicationFrame), new PropertyMetadata(new GridLength(4, GridUnitType.Star)));
public static void SetContentFirstColumnWidth(DependencyObject o, GridLength value)
{
o.SetValue(ContentFirstColumnWidth, value);
}
public static GridLength GetContentFirstColumnWidth(DependencyObject o)
{
return (GridLength)o.GetValue(ContentFirstColumnWidth);
}
#endregion
然后我像这样在我的 ContentPresenter 中使用它
<ContentPresenter library:SlideApplicationFrame.ContentFirstColumnWidth="{TemplateBinding FirstColumnWidth}" Grid.Column="1"/>
最后在样式设置器中
<ColumnDefinition Width="{Binding library:SlideApplicationFrame.ContentFirstColumnWidth}"/>
整个样式设置器如下
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Grid x:Name="GridHeader">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding library:SlideApplicationFrame.ContentFirstColumnWidth}"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
并从 MainPage.xaml.cs 设置
SlideApplicationFrame RootFrame = Window.Current.Content as SlideApplicationFrame;
RootFrame.FirstColumnWidth = new GridLength(4, GridUnitType.Star);
请帮助我,我将不胜感激
【问题讨论】:
-
你想在哪里做:Binding ColumDefinition Width ?
-
我想在这一行做
。只是需要一些帮助,所以我可以自己做 -
你的意思是你需要将它绑定到来自 ViewModel(或后面的代码)的某个值或某个元素的值?
-
来自后面的代码或 ContentPresenter
-
添加了一些提示的答案,如果还不够或不起作用,请告诉我,我会提供一些示例
标签: c# xaml windows-phone-8.1 silverlight-5.0