2017-04-03 50 views
-1

我遇到問題,但我想將樣式(字體大小)應用於我的所有文本框,無論它們在哪裏。將樣式應用於我的WPF應用程序中的所有文本框

當然我發現這些鏈接: Apply an application-level style to all textboxes

How to target all controls (WPF Styles)

但是,這並不爲我工作。

第一個更好,因爲你不需要使用一個鍵,並去所有的文本框。

我已經在我的app.xml中應用於所有控件(應用顏色)的樣式,所以我嘗試了這樣的事情(即使這對我來說不夠實際,因爲這些樣式並不是到處都是):

<Style x:Key="Type1Data" TargetType="{x:Type Control}"> 
    <Setter Property="Background" Value="#FEE9E6"/> 
    <Style.Resources> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="FontSize" Value="50" /> 
     </Style> 
    </Style.Resources> 
</Style> 

而且正如我所說的,我也試過這個,但是不起作用。

<Style TargetType="{x:Type TextBox}"> 
       <Setter Property="FontSize" Value="50" /> 
      </Style> 

任何想法什麼是我的問題,我怎麼能達到我想要的? 我能找到的所有這些讓我回到相同的代碼,並沒有找到一個有效的代碼。

編輯:這裏是我當前的App.xaml

<Application x:Class="myApp.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:myApp" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
    <Style TargetType="TextBox"><!-- tried adding those 3 lines too--> 
     <Setter Property="FontSize" Value="50"/> 
    </Style> 
     <Style x:Key="Type1Data" TargetType="{x:Type Control}"> 
      <Setter Property="Background" Value="Blue"/> 
     </Style> 
     <Style x:Key="Type2Data" TargetType="{x:Type Control}"> 
      <Setter Property="Background" Value="White"/> 
     </Style> 
     <Style x:Key="Type3Data" TargetType="{x:Type Control}"> 
      <Setter Property="Background" Value="Green"/> 
     </Style> 
    </Application.Resources> 
</Application> 

正如我剛纔所說,目前的風格並沒有覆蓋整個應用程序(我補充一點,是需要的密鑰或沒有)

編輯:如果我直接(作爲第二個setter)字體大小添加到類型1,2或3,字體大小適用。所以看起來,除了app.xml中的3以外,沒有其他的風格可以應用。代碼

<Setter Property="FontSize" Value="50" /> 

樣誰應該得到一個不同的文本框的大小(一個已經是一個風格,一個沒有),他們是在網格:

<com:ViewControl x:Class="myApp.View.ViewControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:syncfusion="http://schemas.syncfusion.com/wpf" 
      xmlns:input="http://schemas.syncfusion.com/wpf" 
      xmlns:local="clr-namespace:myApp.View" 
      xmlns:com="clr-namespace:RAPINF.PLL.Common;assembly=myApp.Common" 
      xmlns:entities="clr-namespace:myApp.Entities;assembly=myApp.Entities" 
      mc:Ignorable="d" 
      d:DesignHeight="500" d:DesignWidth="700"> 

<TextBox Style="{StaticResource Type1Data}" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="2" Text="{Binding Data.Name}" VerticalAlignment="Center" /> 

     <TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding Data.Name}" VerticalAlignment="Center" Grid.ColumnSpan="3" /> 

編輯:添加彈出窗口的代碼,我有時使用和工程

<sf:RibbonWindow x:Class="namespace:myApp.Common.DetailViewWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:sf="http://schemas.syncfusion.com/wpf" 
       xmlns:self="clr-namespace:myApp.Common" 
       mc:Ignorable="d" 
       d:Height="300" d:Width="400" 
       WindowStartupLocation="CenterOwner" 
       > 
</sf:RibbonWindow> 

我的用戶是如何加入到碼頭:

public void AddView(UserControl View, string sTitle, DockState docState) 
{ 
    int Width = 800; 
    int Height = 400; 

    DockingManager.SetHeader(View, sTitle); 
    DocumentContainer.SetMDIBounds(View, new Rect(30, 30, Width, Height)); 
    DockingManager.SetState(View, docState); 
    DockingManager.SetShowCloseMenuItem(View, true); 
    DockingManager.SetDesiredWidthInDockedMode(View, Width); 

    DockManager.Children.Add(View); 
    ActivateView(View); 
} 

如果我使用了被告席我之前:

ApplicationContext.Current.AddView(View, DockState.Document); 

,並有彈出,幾乎是相同的:

DetailViewWindow dlg = new DetailViewWindow(View); 
    dlg.ShowDialog(); 

由於,這兩個代碼與相同視圖中使用(是的,完全一樣),那麼我想問題來自在碼頭中添加視圖的事實,而不是在彈出窗口中。

碼頭管理器是否強迫我使用密鑰?

謝謝你的幫助。

+0

你能告訴我們你的App.xaml嗎? –

+0

請提供您的問題的最小,完整和可驗證的示例:https://stackoverflow.com/help/mcve – mm8

+0

@MightyBadaboom補充:因爲沒有比我顯示的更多,我沒有添加整個事情。 mm8:我提供了app.xml,但除此之外,我顯示了我所嘗試的所有內容,結果是,我的文本框的字體大小沒有任何內容。 –

回答

1

我無法重現您的問題。我可以向你展示一個可行的例子。也許這有助於你發現你的問題。

的App.xaml

<Application x:Class="WpfApplication2.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApplication2" 
      StartupUri="MainWindow.xaml"> 
<Application.Resources> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="FontSize" 
        Value="50" /> 
     </Style> 
    </Application.Resources> 
</Application> 

MainWindow.xaml

<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" 
     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
     mc:Ignorable="d" 
     Title="MainWindow" 
     Name="MyWindow" 
     SizeToContent="WidthAndHeight"> 
    <TextBox Width="150"/> 
</Window> 

輸出

Example

+0

是的,那是我找到的代碼(請參閱我的問題的鏈接)。而這正是我的問題:什麼應該起作用,可悲的是沒有。 –

+0

就像mm8說的。您必須提供一個可以重現問題的最小樣本。否則,這幾乎是不可能的。也許在別的地方有一個Style覆蓋了App.xaml中的一個。 –

+0

增加了一個精度:如果我直接添加到類型1,2或3,字體大小適用(樣式中的第二個setter),它的工作原理,所以除了Type1,2或3之外沒有任何東西可以應用。而且由於它不使用Fontsize ...但是由於這三種風格並不是無處不在,我還需要別的東西。 –

相關問題