使用控件模板中的按钮清除wpf列表框选择,不使用代码隐藏

时间:2010-01-13 11:20:02

标签: wpf button listbox styles controltemplate

我想为WPF Style创建一个ListBox,其中包含用户可以点击的Button中的ControlTemplate,并清除ListBox选择。 我不想使用代码隐藏,以便Style可以应用于任何ListBox。 我尝试过使用EventTriggerStoryboard,但事实证明它有问题,因为它只能在第一次运行并停止Storyboard设置之前的选择。 我知道我可以使用用户控件,但我想知道是否可以仅使用Style来实现此目的。

1 个答案:

答案 0 :(得分:1)

使用XAML并且只能使用.NET框架提供的类是不可能实现的。但是,您仍然可以通过定义新命令(称为ClearSelectionCommand)和新附加属性(称为ClearSelectionOnCommand)来生成可重用的解决方案。

然后你可以将这些元素融入你的风格中。

示例:

public class SelectorBehavior
{
    public static RoutedCommand 
        ClearSelectionCommand = 
            new RoutedCommand(
                "ClearSelectionCommand", 
                typeof(SelectorBehavior));

    public static bool GetClearSelectionOnCommand(DependencyObject obj)
    {
        return (bool)obj.GetValue(ClearSelectionOnCommandProperty);
    }

    public static void SetClearSelectionOnCommand(
        DependencyObject obj, 
        bool value)
    {
        obj.SetValue(ClearSelectionOnCommandProperty, value);
    }

    public static readonly DependencyProperty ClearSelectionOnCommandProperty =
        DependencyProperty.RegisterAttached(
            "ClearSelectionOnCommand", 
            typeof(bool), 
            typeof(SelectorBehavior), 
            new UIPropertyMetadata(false, OnClearSelectionOnCommandChanged));

    public static void OnClearSelectionOnCommandChanged(
        DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
    {
        Selector selector = d as Selector;
        if (selector == null) return;
        bool nv = (bool)e.NewValue, ov = (bool)e.OldValue;
        if (nv == ov) return;

        if (nv)
        {
            selector.CommandBindings.Add(
                new CommandBinding(
                    ClearSelectionCommand, 
                    ClearSelectionCommand_Executed, 
                    ClearSelectionCommand_CanExecute));
        }
        else
        {
            var cmd = selector
                        .CommandBindings
                        .Cast<CommandBinding>()
                        .SingleOrDefault(x => 
                            x.Command == ClearSelectionCommand);
            if (cmd != null)
                selector.CommandBindings.Remove(cmd);
        }
    }

    public static void ClearSelectionCommand_Executed(
        object sender, 
        ExecutedRoutedEventArgs e)
    {
        Selector selector = (Selector)sender;
        selector.SelectedIndex = -1;
    }

    public static void ClearSelectionCommand_CanExecute(
        object sender, 
        CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}

示例用法 - XAML:

<Window x:Class="ClearSelectionBehaviorLibrary.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ClearSelectionBehaviorLibrary"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="MyStyle" TargetType="Selector">
            <Setter 
                Property="local:SelectorBehavior.ClearSelectionOnCommand" 
                Value="True"/>
        </Style>
    </Window.Resources>
    <Grid>
        <DockPanel>
            <Button 
                DockPanel.Dock="Bottom"
                Content="Clear"
                Command="{x:Static local:SelectorBehavior.ClearSelectionCommand}"
                CommandTarget="{Binding ElementName=TheListBox}"/>
            <ListBox
                Name="TheListBox" 
                ItemsSource="{Binding MyData}" 
                Style="{StaticResource MyStyle}"/>
        </DockPanel>
    </Grid>
</Window>

示例用法 - 代码背后:

public partial class Window1 : Window
{
    public List<string> MyData { get; set; }

    public Window1()
    {
        MyData = new List<string>
        {
            "aa","bb","cc","dd","ee"
        };
        InitializeComponent();
        DataContext = this;
    }
}