我在文本框中实现了一个按钮,用以下代码覆盖模板:
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="Border"
CornerRadius="2"
Padding="2"
Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1">
<Grid x:Name="LayoutGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" />
<Button x:Name="XButton"
Grid.Column="2"
Width="25"
Height="25"
Content="X"
Visibility="Visible"
BorderBrush="Transparent"
Command="{Binding ButtonClick}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
该按钮应在点击时删除文本框中的所有内容。为此,我想使用命令“ Command =”{Binding ButtonClick}“”
可以在不创建自定义控件和库的情况下完成绑定吗? 或者如何完成绑定或功能?
我正在使用MVVM模式是否有办法例如使用ViewModel并创建一个属性来绑定它?
答案 0 :(得分:1)
对于类似于我使用AttachedCommandBehaviors“ACB”
的样式的绑定http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/
我用它来检测我想要检测点击次数的项目的点击次数。我没有将它用于按钮,但我认为它应该也能正常工作。
我绑定到样式的祖先,其类型是用户控制,可能需要根据您的应用进行更改。
您需要在xaml中添加xmlns:acb命名空间,并查看链接以获取详细信息。
<ControlTemplate TargetType="{x:Type ListViewItem}">
<StackPanel x:Name="IconBorder"
acb:CommandBehavior.Event="PreviewMouseUp"
acb:CommandBehavior.Command="{Binding Path=DataContext.SelectedListItemSingleClickCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
acb:CommandBehavior.CommandParameter="{Binding}">
<DockPanel LastChildFill="False" HorizontalAlignment="Left" Width="{Binding Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}">
<TextBlock Text="{Binding Title}"
DockPanel.Dock="Left"
x:Name="ListItemText" />
<Image x:Name="ActionIcon"
Source="{Binding Path=DataContext.SelectedListActionIcon, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
DockPanel.Dock="Right"
acb:CommandBehavior.Event="PreviewMouseUp"
acb:CommandBehavior.Command="{Binding Path=DataContext.SelectedListActionIconClickCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
acb:CommandBehavior.CommandParameter="{Binding}">
</Image>
</DockPanel>
<ContentPresenter DockPanel.Dock="Left" />
</StackPanel>
</ControlTemplate>