2015-09-28 49 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