从TreeView触发一个按钮(使用双击项目)

时间:2014-04-11 05:57:57

标签: c# wpf

我很少被困在这里,希望有人能帮助我找到正确的方向。

我的wpf应用程序中有这个树视图

  <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80*"></RowDefinition>
            <RowDefinition Height="10*"></RowDefinition>
            <RowDefinition Height="10*"></RowDefinition>
        </Grid.RowDefinitions>
        <TreeView 
            Grid.Row="0" 
            Name="tvTopics"            
            VerticalAlignment="Stretch" 
            HorizontalAlignment="Stretch" 
            MouseDoubleClick="TreeView_MouseDoubleClick" 
            ItemsSource="{Binding TierOneItems}"
            SelectedItemChanged="treeView1_SelectedItemChanged">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding Topic.IsSelected, Mode=TwoWay}" />
                    <!-- 
                    <EventSetter Event="MouseDoubleClick" Handler="itemDoubleClicked" />-->
                    <Setter Property="FontWeight" Value="Normal" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                        <Trigger Property="IsExpanded" Value="True">
                            <Setter Property="IsSelected" Value="True" />        
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <TextBox Name="tbxSearch"  Width="210" Height="25" Margin="10 0 0 0" TextChanged="tbxSearch_TextChanged">
            </TextBox>
            <Button Name="btnSearch" Width="155" Height="25" Margin="5 0 0 0" IsEnabled="False" Click="btnSearch_Click">Search</Button>            
        </StackPanel>
        <StackPanel Grid.Row="2" Orientation="Horizontal">
            <TextBox Text="{Binding SelectedTopic.FullName, Mode=OneWay}" Width="210" Height="25" IsReadOnly="True" Margin="10 0 0 0"></TextBox>
            <Button Width="60" Height="25" Margin="5 0 0 0" Command="{Binding AddTopicCommand}" CommandParameter="{Binding SelectedTopic}">Choose</Button>
            <Button Name="btnClose" Width="60" Height="25" Margin="5 0 0 0" Click="btnClose_Click">Close</Button>
        </StackPanel>
    </Grid>

我试图找到一种方法,当鼠标在树状视图中双击时,它将触发视图中的按钮点击。

<Button Width="60" Height="25" Margin="5 0 0 0" Command="{Binding AddTopicCommand}" CommandParameter="{Binding SelectedTopic}">Choose</Button>

1 个答案:

答案 0 :(得分:1)

我认为,您需要利用命令,因为它们可以从不同的UI源调用 - 这是它们的主要目的。

在这种情况下,可以使用Interactivity命名空间中的EventTriggerInvokeCommandAction,它允许您在所需事件上调用Command。

Note:不要忘记添加对System.Windows.Interactivity程序集的引用。

以下是示例:

XAML

<Window x:Class="TreeViewCommandHelp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <TreeView Name="MyTreeView">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick"
                                SourceObject="{Binding ElementName=MyTreeView}">

                    <i:InvokeCommandAction Command="{Binding Path=TestCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <TreeViewItem Header="North America">
                <TreeViewItem Header="USA" />
                <TreeViewItem Header="Canada" />
                <TreeViewItem Header="Mexico" />
            </TreeViewItem>

            <TreeViewItem Header="South America">
                <TreeViewItem Header="Argentina" />
                <TreeViewItem Header="Brazil" />
                <TreeViewItem Header="Uruguay" />
            </TreeViewItem>
        </TreeView>

        <Button Width="110"
                Height="25" 
                Content="Change command"
                Command="{Binding Path=TestCommand}" />
    </Grid>
</Window>

Code-behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;
    }

    private ICommand _testCommand = null;

    public ICommand TestCommand
    {
        get
        {
            if (_testCommand == null)
            {
                _testCommand = new RelayCommand(param => this.Test(), null);
            }

            return _testCommand;
        }
    }

    private void Test()
    {
        MessageBox.Show("Test command...");
    }
}

public class RelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute) : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }

        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }
}