3

我有一个基于 MVVM 的复杂 WPF 应用程序。我想为我的应用程序创建一个辅助模式。例如,我认为当用户打开屏幕时自定义的提示将打开(在文本框上)并保持打开状态,直到用户将一些数据放入其中。当用户放置数据时,焦点将自动转移到下一个控件,并且该控件的另一个工具提示将打开,这样流程将继续。一件事,我只想在 XAML 中编码..任何建议朋友???

4

1 回答 1

1

我认为在附加属性的帮助下修改文本框的模板会更容易。

创建一个类来保存辅助附加属性。让我们调用它Assistance,它将有两个附加属性:

  1. 类型的帮助提示内容object
  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}"
         ..../>

这从样式中删除了数据触发器(以查看模型数据)并使其成为非常通用的方法。

[这里不处理焦点的变化,我知道你已经做到了。]

于 2013-05-27T22:15:12.787 回答