我有一个基于MVVM的复杂WPF应用程序。我想为我的应用程序创建一个辅助模式。例如,我认为当用户打开屏幕并保持打开时,将打开一个自定义的tootip(在文本框上),直到用户将一些数据放入其中。当用户放置数据时,焦点将自动转移到下一个控件,并且该控件的另一个工具提示将打开,这样流程将继续。一件事,我只想在XAML中编码。任何建议朋友??? < / p>
答案 0 :(得分:1)
我认为在附加属性的帮助下修改文本框的模板会更容易。
创建一个类来保存辅助附加属性。我们称之为Assistance
,它将有两个附加属性:
object
的HelpTipContent。bool
的IsAssistanceActive。然后是文本框的自定义样式/模板(基于默认的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}"
..../>
这将从样式中删除数据触发器(以查看模型数据),并使其成为非常通用的方法。
[这里没有处理焦点的变化,我知道你已经完成了。]