2014-01-16 34 views
1

我正在製作一個帶有TextBlock指標的縮放控件(Slider),它可以告訴你當前的比例因子是(有點像在Word的右下角)。我有兩天的時間學習WPF,我已經能夠弄清楚如何去做大部分的事情,但是我覺得有一種更簡單的方法(一種可能只涉及XAML-端的代碼,而不是一束鼠標事件被捕獲。在懸停時更改TextBlock外觀的簡單方法?

我想用於當懸停(暗示可點擊)和點擊到滑塊元件復位到1.0加下劃線的文本。

這是我有:

<StatusBarItem Grid.Column="1" HorizontalAlignment="Right"> 
    <Slider x:Name="mapCanvasScaleSlider" Width="150" Value="1" Orientation="Horizontal" HorizontalAlignment="Left" 
      IsSnapToTickEnabled="True" Minimum="0.25" Maximum="4" TickPlacement="BottomRight" 
      Ticks="0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4"/> 
</StatusBarItem> 
<StatusBarItem Grid.Column="2"> 
    <TextBlock Name="zoomIndicator" Text="{Binding ElementName=mapCanvasScaleSlider,Path=Value,StringFormat=0%}" 
       MouseDown="ResetZoomWindow" MouseEnter="zoomIndicator_MouseEnter" MouseLeave="zoomIndicator_MouseLeave" 
       ToolTip="Zoom level; click to reset"/> 
</StatusBarItem> 

 

private void ResetZoomWindow(object sender, MouseButtonEventArgs args) 
    { 
     mapCanvasScaleSlider.Value = 1.0; 
    } 

    private void zoomIndicator_MouseLeave(object sender, MouseEventArgs e) 
    { 
     zoomIndicator.TextDecorations = TextDecorations.Underline; 
    } 

    private void zoomIndicator_MouseLeave(object sender, MouseEventArgs e) 
    { 
     zoomIndicator.TextDecorations = null; 
    } 

我覺得好像有一種更好的方式,通過XAML來做到這一點,而不是有三個獨立的.cs端功能。

回答

1

你可以使用樣式觸發的文本塊,像這樣的其他職務How to set MouseOver event/trigger for border in XAML?

描述工作液:

<StatusBarItem Grid.Column="2"> 
    <TextBlock Name="zoomIndicator" Text="{Binding ElementName=mapCanvasScaleSlider,Path=Value,StringFormat=0%}" 
       MouseDown="ResetZoomWindow" ToolTip="Zoom level; click to reset"> 
     <TextBlock.Style> 
      <Style> 
       <Setter Property="TextBlock.TextDecorations" Value="" /> 
       <Style.Triggers> 
        <Trigger Property="TextBlock.IsMouseOver" Value="True"> 
         <Setter Property="TextBlock.TextDecorations" Value="Underline" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 
</StatusBarItem> 

還是要手動重置縮放級別(我認爲),但。

+0

似乎已經做的伎倆。更好的是,我甚至不需要使用邊框!我將用工作代碼編輯你的答案。 – Benjin

0

你可以使用VisualState(如果你使用Blend它很容易編輯)。 我個人比較喜歡的風格的觸發器,除非我必須添加故事板動畫 - 然後我見於Ger.Offen使用的VisualState

about VisualState

0

通常情況下,你不希望使用TextBlock作爲一個按鈕(儘管當然你也可以) 。您可能需要考慮使用更合適的控件,如ButtonHyperlinkButton,這些控件的基本鼠標事件處理已經連線。然後,您可以應用您喜歡的任何款式。一個Button控制,例如,可以很容易地styled重新模板作爲TextBlock用下劃線鼠標懸停:

<Style TargetType="Button" x:Key="LinkButtonStyle"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard Duration="0:0:0.1"> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="content" Storyboard.TargetProperty="TextDecorations"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <TextDecorationCollection>Underline</TextDecorationCollection> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <TextBlock x:Name="content" Text="{TemplateBinding Content}" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

通過引用樣式鍵使用它:

<Button Content="click" Style="{StaticResource LinkButtonStyle}" /> 

使用這種方法(而不是將觸發器添加到TextBlock的替代方法)帶來了一些內置於Button控件的優點。

  1. 您可以將樣式應用於複合像Pressed
  2. 可以使用Click事件狀態,其相關Command財產