2013-05-26 37 views
3

我有一個基於MVVM的複雜WPF應用程序。我想爲我的應用程序創建一個輔助模式。例如,我想在用戶打開屏幕時打開一個定製的tootip(在文本框中),並保持打開狀態,直到用戶將一些數據放入它爲止。當用戶放置數據時,焦點將自動轉移到下一個控件,並且該控件的另一個工具提示將打開,這樣流程將繼續。一件事,我只想在XAML中編寫代碼。任何建議朋友?在用戶將數據放入文本框之前,在文本框的焦點上顯示工具提示

+0

你可以預先定義自己的提示,並適當約束他們,然後使用觸發器來迫使焦點從一個控制到另一個一旦數據輸入到正確的文本塊中。你嘗試過那樣的事嗎? – aqua

+0

是的工具提示是預先定義好的,並且是合適的。我爲工具提示找到了一個通用樣式,然後我將這個樣式應用到所有的文本塊。我已經使用了一個觸發器來綁定一個布爾屬性(如下所示)。@aqua –

回答

1

我認爲使用附加屬性的一些幫助來修改文本框的模板會更容易。

做一個類來保存助手附加屬性。讓我們把它Assistance,它有兩個附加屬性:

  1. object型AssistanceTipContent。
  2. IsAssistanceActive類型bool

然後自定義樣式/模板文本框(基於默認Aero主題):

<!--Add this xmlns definition to the root of the file--> 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
.... 
<!--Then in resources--> 
<LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0"> 
    <GradientStop Color="#ABADB3" Offset="0.05"/> 
    <GradientStop Color="#E2E3EA" Offset="0.07"/> 
    <GradientStop Color="#E3E9EF" Offset="1"/> 
</LinearGradientBrush> 
<Style TargetType="{x:Type TextBox}"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="AllowDrop" Value="true"/> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Grid> 
        <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"> 
         <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Microsoft_Windows_Themes:ListBoxChrome> 
        <Popup x:Name="AssistanceTip" 
          IsOpen="False" 
          AllowsTransparancy="True"> 
         <!--TODO: Add Background/BorderBrush/BorderThicknes to this Border so it matches ToolTip style--> 
         <Border> 
          <ContentControl Content="{Binding Path=(Assistance.AssistanceTipContent), RelativeSource={RelativeSource TemplatedParent}}"/> 
         </Border> 
        </Popup> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <MultiDataTrigger> 
         <MultiDataTrigger.Conditions> 
          <Condition Binding="{Binding RelativeSource={RelativeSource Self}, IsKeyboardFocusWithin}" Value="True"/> 
          <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Text}" Value=""/> 
          <Condition Binding="{Binding Path=(Assistance.IsAssistanceActive), RelativeSource={RelativeSource Self}}" Value="True"/> 
         </MultiDataTrigger.Conditions> 
         <Setter TargetName="AssistanceTip" Property="IsOpen" Value="True"/> 
        </MultiDataTrigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

現在你的使用將是這樣的:

<TextBox local:Assistance.AssistanceTipContent="Some text to help the user" 
     local:Assistance.IsAssistanceActive="{Binding IsAssistanceModeActive}" 
     ..../> 

這消除了數據觸發器(從樣式中查看模型數據)並使其成爲非常通用的方法。

[焦點的變化不是這裏處理,我明白,你已經做到了這一點。]

相關問題