2015-11-24 43 views
0

我是WPF的初學者。你知道這很難! 好的,我對我的應用程序有一點風格(實際上我複製它,真的不知道在中間發生了什麼,所以...) 我想添加一些超鏈接和按鈕到應用程序的標題。所以我編輯了風格並添加了那些風格。 但現在我想更改代碼隱藏的內容,但是因爲您已經知道這種情況沒有發生! 這是我的風格在Application.Resources:從代碼隱藏(WPF)添加控制器到<Style>

<BooleanToVisibilityConverter x:Key="bool2VisibilityConverter" /> 
      <Color x:Key="WindowBackgroundColor">#FF2D2D30</Color> 
      <Color x:Key="HighlightColor">Orange</Color> 
      <Color x:Key="BlueColor">AntiqueWhite</Color> 
      <Color x:Key="ForegroundColor">#FFF4F4F5</Color> 

      <SolidColorBrush x:Key="WindowBackgroundColorBrush" Color="{StaticResource WindowBackgroundColor}"/> 
      <SolidColorBrush x:Key="HighlightColorBrush" Color="{StaticResource HighlightColor}"/> 
      <SolidColorBrush x:Key="BlueColorBrush" Color="{StaticResource BlueColor}"/> 
      <SolidColorBrush x:Key="ForegroundColorBrush" Color="{StaticResource ForegroundColor}"/> 

      <Style x:Key="WindowButtonStyle" TargetType="{x:Type Button}"> 
       <Setter Property="Foreground" Value="{DynamicResource ForegroundColorBrush}" /> 
       <Setter Property="Background" Value="Transparent" /> 
       <Setter Property="HorizontalContentAlignment" Value="Center" /> 
       <Setter Property="VerticalContentAlignment" Value="Center" /> 
       <Setter Property="Padding" Value="1" /> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type Button}"> 
          <Grid Background="{TemplateBinding Background}"> 
           <ContentPresenter x:Name="contentPresenter" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
          Margin="{TemplateBinding Padding}" 
          RecognizesAccessKey="True" /> 
          </Grid> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Background" Value="{StaticResource HighlightColorBrush}" /> 
           </Trigger> 
           <Trigger Property="IsPressed" Value="True"> 
            <Setter Property="Background" Value="{DynamicResource BlueColorBrush}" /> 
           </Trigger> 
           <Trigger Property="IsEnabled" Value="false"> 
            <Setter TargetName="contentPresenter" Property="Opacity" Value=".5" /> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 

      <Style x:Key="{x:Type Hyperlink}" TargetType="Hyperlink"> 
       <Setter Property="TextDecorations" Value="{x:Null}"/> 
       <Setter Property="Foreground" Value="#FFF4F4F5"/> 
       <Setter Property="FontSize" Value="14"/> 
      </Style> 
      <Style x:Key="MyWindowStyle" TargetType="local:MainWindow"> 
       <Setter Property="Foreground" Value="{DynamicResource ForegroundColorBrush}" /> 
       <Setter Property="Background" Value="{DynamicResource WindowBackgroundBrush}"/> 
       <Setter Property="ResizeMode" Value="NoResize" /> 
       <Setter Property="UseLayoutRounding" Value="True" /> 
       <Setter Property="TextOptions.TextFormattingMode" Value="Display" /> 
      <Setter Property="WindowChrome.WindowChrome"> 
       <Setter.Value> 
        <WindowChrome CornerRadius="0" GlassFrameThickness="1" UseAeroCaptionButtons="False"/> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/> 
    <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="local:MainWindow"> 
          <Border x:Name="WindowBorder" Margin="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}}" Background="{StaticResource WindowBackgroundColorBrush}"> 
           <Grid> 
            <Border BorderThickness="1"> 
             <AdornerDecorator> 
              <Grid x:Name="LayoutRoot"> 
               <Grid.RowDefinitions> 
                <RowDefinition Height="27" /> 
                <RowDefinition Height="*" /> 
                <RowDefinition Height="15" /> 
               </Grid.RowDefinitions> 
               <ContentPresenter Grid.Row="1" Grid.RowSpan="2" Margin="7"/> 
               <Image Width="30" Height="30" /> 
<!-- This is where i want put those Hyperlinks and the Button --> 
               <TextBlock Margin="50,5,0,0"> 
                <Hyperlink NavigateUri="SupportPage.xaml" RequestNavigate="Hyperlink_Support">Support</Hyperlink> 
               </TextBlock> 
               <TextBlock Margin="122,5,0,0"> 
                <Hyperlink NavigateUri="HelpPage.xaml" RequestNavigate="Hyperlink_Help">Support</Hyperlink> 
               </TextBlock> 
               <Button Name="btCustom" Content="btThatIWantChangeItsContent" VerticalAlignment="Bottom" HorizontalAlignment="Left"/> 
               <TextBlock Margin="220,5,0,0"> 
                <Hyperlink NavigateUri="AboutPage.xaml" RequestNavigate="Hyperlink_About">About</Hyperlink> 
               </TextBlock> 
<!-- to here --> 
<Rectangle Mouse.MouseDown="rectangleMoveWindow_MouseDown" Margin="300,0,0,0" x:Name="HeaderBackground" Height="25" Fill="{DynamicResource WindowBackgroundColorBrush}" VerticalAlignment="Top" Grid.Row="0"/> 
              <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" WindowChrome.IsHitTestVisibleInChrome="True" Grid.Row="0"> 
               <Button Command="{Binding Source={x:Static SystemCommands.MinimizeWindowCommand}}" ToolTip="minimize" Style="{StaticResource WindowButtonStyle}"> 
                <Button.Content> 
                 <Grid Width="30" Height="25" RenderTransform="1,0,0,1,0,1"> 
                  <Path Data="M0,6 L8,6 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center" 
                 Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="2" /> 
                 </Grid> 
                </Button.Content> 
               </Button> 
               <Grid Margin="1,0,1,0"> 
                <Button x:Name="Restore" Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}" ToolTip="restore" Visibility="Collapsed" Style="{StaticResource WindowButtonStyle}"> 
                 <Button.Content> 
                  <Grid Width="30" Height="25" UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5"> 
                   <Path Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center" 
                  Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1" /> 
                  </Grid> 
                 </Button.Content> 
                </Button> 
                <Button x:Name="Maximize" Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}" ToolTip="maximize" Style="{StaticResource WindowButtonStyle}"> 
                 <Button.Content> 
                  <Grid Width="31" Height="25"> 
                   <Path Data="M0,1 L9,1 L9,8 L0,8 Z" Width="9" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center" 
                  Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="2" /> 
                  </Grid> 
                 </Button.Content> 
                </Button> 
               </Grid> 
               <Button Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}" ToolTip="close" Style="{StaticResource WindowButtonStyle}"> 
                <Button.Content> 
                 <Grid Width="30" Height="25" RenderTransform="1,0,0,1,0,1"> 
                  <Path Data="M0,0 L8,7 M8,0 L0,7 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center" 
                 Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1.5" /> 
                 </Grid> 
                </Button.Content> 
               </Button> 
              </StackPanel> 
              <Grid Grid.Row="2"> 
               <Path x:Name="ResizeGrip" Visibility="Collapsed" Width="12" Height="12" Margin="1" HorizontalAlignment="Right" 
             Stroke="{StaticResource BlueColorBrush}" StrokeThickness="1" Stretch="None" Data="F1 M1,10 L3,10 M5,10 L7,10 M9,10 L11,10 M2,9 L2,11 M6,9 L6,11 M10,9 L10,11 M5,6 L7,6 M9,6 L11,6 M6,5 L6,7 M10,5 L10,7 M9,2 L11,2 M10,1 L10,3" /> 
              </Grid> 
             </Grid> 
            </AdornerDecorator> 
           </Border> 
           <Border BorderBrush="{StaticResource BlueColorBrush}" BorderThickness="1" Visibility="{Binding IsActive, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Converter={StaticResource bool2VisibilityConverter}}" /> 
          </Grid> 
         </Border> 
<ControlTemplate.Triggers> 
          <Trigger Property="WindowState" Value="Maximized"> 
           <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" /> 
           <Setter TargetName="Restore" Property="Visibility" Value="Visible" /> 
           <Setter TargetName="LayoutRoot" Property="Margin" Value="7" /> 
          </Trigger> 
          <Trigger Property="WindowState" Value="Normal"> 
           <Setter TargetName="Maximize" Property="Visibility" Value="Visible" /> 
           <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" /> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="ResizeMode" Value="CanResizeWithGrip" /> 
            <Condition Property="WindowState" Value="Normal" /> 
           </MultiTrigger.Conditions> 
           <Setter TargetName="ResizeGrip" Property="Visibility" Value="Visible" /> 
          </MultiTrigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

,所以我不知道該怎麼超鏈接的內容變化,甚至我可以用按鈕替換它們,如果這是必要的。告訴我如何改變這些內容。當然,我不得不說我嘗試過FindResource,但找不到超鏈接或按鈕。先謝謝你們。

回答

0
<StackPanel x:Name="panel" xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
    <StackPanel.Resources> 
     <sys:String x:Key="help">Support</sys:String> 
    </StackPanel.Resources> 
    <Label DataContext="{StaticResource help}"> 
     <Hyperlink><TextBlock Text="{DynamicResource help}"/></Hyperlink> 
    </Label> 
    <Button Content="OK" Click="button_Click"/> 
</StackPanel> 

private void button_Click(object sender, RoutedEventArgs e) 
{ 
    panel.Resources["help"] = "Help"; 
} 
+0

謝謝,這是偉大的。但還沒有安靜呢!它適用於StackPanel之類的東西,並從該窗口調用它。但如果我想改變它的App.xaml例如,我無法從任何地方訪問它!現在想想它像是另一件事情在Application.Resources中的一些東西。就像Application.Resources中Style內的TextBlock一樣!我知道......這太瘋狂了!如果你能幫助我,我很感激。再次感謝 – Shantia

+0

xmlns:sys =「clr-namespace:System; assembly = mscorlib」 –

+0

@EISHEN *** xmlns:sys =「clr-namespace:System; assembly = mscorlib」***可以添加到任何* XAML UIElement *部分包括*應用程序* –

0

試試這個:
1.創建一個新的WpfApplication項目
2. 刪除MainWindow.xaml
3.添加頁面:startup.xaml,或的Page1.xaml更多,並設置diff背景
4.更改StartupUri =「startup.xaml」 in App.xaml
5. 添加NavigationWindow風格的App.xaml

<Style TargetType="{x:Type NavigationWindow}"> 
    <Setter Property="OverridesDefaultStyle" Value="True"/> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type NavigationWindow}"> 
       <Border Background="{TemplateBinding Background}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="*"/> 
         </Grid.RowDefinitions> 
         <AdornerDecorator Grid.Row="1"> 
          <ContentPresenter Name="PART_NavWinCP" ClipToBounds="true" /> 
         </AdornerDecorator> 
         <StackPanel Grid.Row="0" Margin="20" Orientation="Horizontal"> 
          <Label> 
           <Hyperlink NavigateUri="Page1.xaml">Page1</Hyperlink> 
          </Label> 
         </StackPanel> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

謝謝你,但我認爲你讓我錯了。此代碼的工作原理與我在第一篇文章中粘貼的代碼一樣。現在想想這樣:我想改變** Page1的NavigateUri ** **代碼隱藏的超鏈接或類似的東西。我怎樣才能做到這一點? – Shantia

+0

哦...我很抱歉,我刪除了我的第一個答案,我認爲這就是你想要的。 –