Ing*_*als 7 wpf layout styles resourcedictionary
我正在尝试创建一个应用选项卡,其中每个选项卡都有一个按钮区域和一个视图区域.
现在每个选项卡在布局中基本上具有相同的布局,我希望能够重用相同的布局,这样我就不必在很多地方进行更改(这只是不好的编程).我可以使用资源或样式完成此操作.
如果可能,请提供灯光代码示例.
编辑:我已经决定添加一个我正在尝试做的例子,因为我还没有得到它.
在每个TabItem下我试图重新创建这个网格(它有点复杂,但你明白了):
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Margin="10"
BorderBrush="{StaticResource MediumColorBrush}"
CornerRadius="10"
BorderThickness="2"
Grid.Row="0">
<!-- First content goes here -->
</Border>
<Border Margin="10"
BorderBrush="{StaticResource MediumColorBrush}"
CornerRadius="10"
BorderThickness="2"
Grid.Row="1">
<!-- Second content goes here -->
</Border>
</Grid>
Run Code Online (Sandbox Code Playgroud)
你也可以看到2个边框是一样的.现在我需要将一些内容占位符放在我的评论所在的位置.我不想在资源字典中声明这个Grid布局,然后在我使用它的地方将单独的内容放入每个边框.
我可能有很多TabItems,所以重复这段代码不是一个好主意,每个Tab页面将在2个占位符中有不同的内容.
我可以用了
<ContentPresenter Content="{Binding}" />
Run Code Online (Sandbox Code Playgroud)
只有1个内容的东西,当会有更多内容时会发生什么.
英戈,
MSDN 上始终提供代码。检查这个:UserControl,自定义控件,DataTemplates。
以下是每种方法的一些示例。为简单起见,让我们假设您要复制的布局是一行带有绿色前景的文本(实际上它可能真的不同,但您明白了)。
1. 用户控制
Xml:
<UserControl x:Class="WpfApplication1.GreenTextUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock x:Name="txt" Foreground="Green"/>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
C#:
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class GreenTextUserControl : UserControl
{
public string Text
{
get { return txt.Text;}
set { txt.Text = value; }
}
public GreenTextUserControl()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
标签控制:
<TabControl>
<TabItem Header="Tab 1">
<loc:GreenTextUserControl Text="This is Tab 1"/>
</TabItem>
<TabItem Header="Tab 2">
<loc:GreenTextUserControl Text="This is Tab 2"/>
</TabItem>
<TabItem Header="Tab 3">
<loc:GreenTextUserControl Text="This is Tab 3"/>
</TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
2. 自定义控件
C#:
public class GreenTextBlock : TextBlock
{
public GreenTextBlock()
{
Foreground = Brushes.Green;
}
}
Run Code Online (Sandbox Code Playgroud)
标签控件:
<TabControl>
<TabItem Header="Tab 1">
<loc:GreenTextBlock Text="This is Tab 1"/>
</TabItem>
<TabItem Header="Tab 2">
<loc:GreenTextBlock Text="This is Tab 2"/>
</TabItem>
<TabItem Header="Tab 3">
<loc:GreenTextBlock Text="This is Tab 3"/>
</TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
如果您的布局比 textblock 更复杂,自定义控件也允许您在 XAML 中定义它,但它与 UserControls 不同。
3. 数据模板
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<x:Array x:Key="GreenText" Type="{x:Type System:String}">
<System:String>This is Tab 1</System:String>
<System:String>This is Tab 2</System:String>
<System:String>This is Tab 3</System:String>
</x:Array>
<!--Tab item content data template-->
<DataTemplate x:Key="GreenTextTemplate">
<TextBlock Text="{Binding}" Foreground="Green"/>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl ItemsSource="{StaticResource GreenText}">
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="ContentTemplate" Value="{StaticResource GreenTextTemplate}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
就是这样 :)。希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
3973 次 |
| 最近记录: |