這應該做的工作,以及我想。有一些編碼留給你雖然
用法:
<Window x:Class="ResponsiveWpfLayout.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:responsiveWpfLayout="clr-namespace:ResponsiveWpfLayout"
Title="MainWindow" Height="350" Width="525">
<Grid>
<responsiveWpfLayout:ResponsiveLayout>
<responsiveWpfLayout:ResponsiveLayout.DefaultTemplate>
<ControlTemplate>
<Border Background="Red">
<TextBlock>Layout A</TextBlock>
</Border>
</ControlTemplate>
</responsiveWpfLayout:ResponsiveLayout.DefaultTemplate>
<responsiveWpfLayout:ResponsiveLayout.AlternativeTemplate>
<ControlTemplate>
<Border Background="Green">
<TextBlock>Layout B</TextBlock>
</Border>
</ControlTemplate>
</responsiveWpfLayout:ResponsiveLayout.AlternativeTemplate>
</responsiveWpfLayout:ResponsiveLayout>
</Grid>
</Window>
ResponsiveLayout.cs
public class ResponsiveLayout : Control
{
static ResponsiveLayout()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ResponsiveLayout), new FrameworkPropertyMetadata(typeof(ResponsiveLayout)));
}
public static readonly DependencyProperty DefaultTemplateProperty = DependencyProperty.Register(
"DefaultTemplate", typeof (ControlTemplate), typeof (ResponsiveLayout), new PropertyMetadata(default(ControlTemplate)));
public ControlTemplate DefaultTemplate
{
get { return (ControlTemplate) GetValue(DefaultTemplateProperty); }
set { SetValue(DefaultTemplateProperty, value); }
}
public static readonly DependencyProperty AlternativeTemplateProperty = DependencyProperty.Register(
"AlternativeTemplate", typeof (ControlTemplate), typeof (ResponsiveLayout), new PropertyMetadata(default(ControlTemplate)));
public ControlTemplate AlternativeTemplate
{
get { return (ControlTemplate) GetValue(AlternativeTemplateProperty); }
set { SetValue(AlternativeTemplateProperty, value); }
}
public static readonly DependencyProperty ActiveTemplateProperty = DependencyProperty.Register(
"ActiveTemplate", typeof (ControlTemplate), typeof (ResponsiveLayout), new PropertyMetadata(default(ControlTemplate)));
public ControlTemplate ActiveTemplate
{
get { return (ControlTemplate) GetValue(ActiveTemplateProperty); }
set { SetValue(ActiveTemplateProperty, value); }
}
protected override Size ArrangeOverride(Size arrangeBounds)
{
if (arrangeBounds.Width > arrangeBounds.Height)
{
ActiveTemplate = DefaultTemplate;
}
else
{
ActiveTemplate = AlternativeTemplate;
}
return base.ArrangeOverride(arrangeBounds);
}
public ResponsiveLayout()
{
ActiveTemplate = DefaultTemplate;
}
}
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ResponsiveWpfLayout">
<Style TargetType="{x:Type local:ResponsiveLayout}">
<Setter Property="DefaultTemplate">
<Setter.Value>
<ControlTemplate>
<Border Background="Orange">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="AlternativeTemplate">
<Setter.Value>
<ControlTemplate>
<Border Background="Blue">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ResponsiveLayout}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentControl Template="{TemplateBinding ActiveTemplate}"></ContentControl>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
來源
2015-10-15 09:39:18
Dbl
這只是一個想法,但你可以嘗試使用連接在網格上的屬性,並有你自己的代碼操縱網格的子節點。 (使他們根據比例改變他們的Grid.Row/Column值,值得一試,這就是我想要做的事情,另一種選擇是模板選擇器 – Dbl
我在嘗試你的第一個建議之前,提供一個模板選擇器技術的小例子?謝謝 – sam