2015-09-28 52 views
1

我是WPF的新手,主要是Winforms和Webforms体验。我正在尝试学习WPF,并且我正试图学习的一件事是在XAML中创建漂亮的用户界面。我一直在尝试复制StaffLynx应用程序的UI。的屏幕截图在座如何在WPF中设计自定义样式的窗口?

http://nextver.com/site/portfolio/stafflynx/

在WPF我想不通,会是怎样创造的窗口中的占位符容器的最佳途径。在上面的链接中,您可以看到所有页面(视图)都被加载到自定形状的窗口中。我怎样才能创建一个这样的可重用窗口?

我应该重写一些控件的模板吗? 总之,我不确定什么是正确的方式来创建一个自定义形状的窗口,如StaffLynx应用程序使用的窗口。

请指教。

+0

我想我可以设计定制的窗口样式和隐藏的按钮来达到我想要的这里提到 HTTP:/ /www.kirupa.com/blend_wpf/custom_wpf_windows.htm –

+0

在这些屏幕截图中,我没有看到任何自定义形状的窗口。你仍然可以通过最常见的min/max/close按钮在顶部看到win7 aero窗口....但是你可以通过编辑样式模板找到正确的方式来定制你想要的东西。 –

+0

您是否正在寻找某种只有一个窗口并且可以更改内容的母版页? – Lance

回答

0

噢,好吧,如果你只是想要很多例子之一如何做这样的事情。下面是一个简单的例子,说明如何使用Clip来削减这个角落,给它一个镜头。希望能帮助到你。

<Window x:Class="NestedCutCornerWindowCWSO" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="NestedCutCornerWindowCWSO" Height="500" Width="800"> 

    <Grid Height="350" Width="500"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <Rectangle Fill="Navy" 
        Clip="M0,0 L485,0 500,15 500,100 0,100 z"/> 

     <TextBlock Foreground="White" FontSize="20" Text="Something" Margin="5"/> 

     <Rectangle Grid.Row="1" 
        Fill="White" 
        Stroke="Navy" StrokeThickness="2"/> 

     <TextBlock Grid.Row="1" Foreground="Black" FontSize="30" 
        HorizontalAlignment="Center" VerticalAlignment="Center" 
        Text="Some Other Stuff..."/> 
    </Grid> 

</Window> 
+0

谢谢克里斯!这完美地告诉我如何创建一个类似的风格的窗口。你能请我解释一下如何用这个设计创建一个模板并重新使用它来加载控件? –

+0

我想更简单的方法是在我构建的每个页面中使用此XAML,但这种方式违反了DRY原则。如果我稍后做出改变,我将不得不到处改变,所以我认为一定有更好的办法。 –

+0

有很多教程可以通过快速谷歌找到,它将向您展示如何为wpf创建自定义窗口样式模板,您只需使用上面显示的模板创建自定义样式并使用'TargetType =“{x :Type Window}'覆盖默认值,你也可以创建一个新的控件或者其他几个选项,如果你有任何麻烦,我们会自己先给它一个(教程会引导你),然后回来。我会尽力避免在问题中混合回答,并且宁愿人们至少首先给Google提供一些东西来学习,干杯 –

1

也许你应该尝试使用ContentTemplateSelector。这里有一个很好的example ..

下面是一个简单的例子,我做了适合您的场景。我有一个有边框的窗口,边框内有一个ContentControl,它有一个模板选择器,可以让你选择要显示的视图。

这里的观点:

看看当地:MyContentTemplateSelector标签。

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <Grid.Resources> 
     <DataTemplate x:Key="FirstTemplate"> 
     <TextBlock Text="First" /> 
     </DataTemplate> 
     <DataTemplate x:Key="SecondTemplate"> 
     <TextBlock Text="Second" /> 
     </DataTemplate> 
     <local:MyContentTemplateSelector FirstTemplate="{StaticResource FirstTemplate}" SecondTemplate="{StaticResource SecondTemplate}" 
             x:Key="mytemplateSelector" /> 
    </Grid.Resources> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Border BorderThickness="1" BorderBrush="Red" Grid.Row="0"> 
     <ContentControl ContentTemplateSelector="{StaticResource mytemplateSelector}" Content="{Binding SelectedViewModel}"/> 
    </Border> 
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"> 
     <Button Command="{Binding SelectFirstViewModel}">Go to First Template</Button> 
     <Button Command="{Binding SelectSecondViewModel}">Go to Second Template</Button> 
    </StackPanel> 
    </Grid> 
</Window> 

这里的视图模型:

public class MainVm : ViewModelBase 
    { 
    private FirstVm _FirstViewModel; 
    public FirstVm FirstViewModel 
    { 
     get { return _FirstViewModel; } 
     set { Set(ref _FirstViewModel, value); } 
    } 

    private SecondVm _SecondViewModel; 
    public SecondVm SecondViewModel 
    { 
     get { return _SecondViewModel; } 
     set { Set(ref _SecondViewModel, value); } 
    } 


    private ViewModelBase _SelectedViewModel; 
    public ViewModelBase SelectedViewModel 
    { 
     get { return _SelectedViewModel; } 
     set { Set(ref _SelectedViewModel, value); } 
    } 

    public ICommand SelectFirstViewModel 
    { 
     get 
     { 
     return new RelayCommand(() => { this.SelectedViewModel = FirstViewModel; }); 
     } 
    } 

    public ICommand SelectSecondViewModel 
    { 
     get 
     { 
     return new RelayCommand(() => { this.SelectedViewModel = SecondViewModel; }); 
     } 
    } 

    public MainVm() 
    { 
     FirstViewModel = new FirstVm(); 
     SecondViewModel = new SecondVm(); 
     SelectedViewModel = this.FirstViewModel; 
    } 
    } 

这些可能是你有你的网页的任何视图模型:

public class FirstVm : ViewModelBase 
    { 

    } 

    public class SecondVm : ViewModelBase 
    { 

    } 

而这里的模板选择。这是重要的部分。无论何时您更改ContenControl的内容,在这种情况下,内容都会绑定到MainVm的SelectedViewmodel属性,此类中的SelectTemplate方法将被调用。这就是你放置视图或数据模板显示的逻辑的地方。

public class MyContentTemplateSelector : DataTemplateSelector 
    { 
    public DataTemplate FirstTemplate { get; set; } 
    public DataTemplate SecondTemplate { get; set; } 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     if (item is FirstVm) 
     return FirstTemplate; 
     if (item is SecondVm) 
     return SecondTemplate; 
     return null; 
    } 
    } 

它看起来象是这样的: enter image description here

enter image description here

+0

谢谢Lawrence,内容模板选择器对我来说是一个新概念。我发现它非常有用。 –

+0

不客气。这是否回答你的问题? – Lance