WPF C#Scrollviewer禁用工具栏按钮

时间:2018-11-16 22:07:29

标签: c# wpf button toolbar scrollviewer

我正在创建C#WPF应用程序。我已将Scrollviewer放置在网格单元内。当应用程序运行时,如果我在Scrollviewer单元内单击任何位置,则位于不同单元中并由不同用户控件控制的所有工具栏按钮都会突然被禁用,而不会重新启用。

为什么会这样,我该如何解决?

感谢您的帮助。

这是我的ScrollViewer代码:

            <ScrollViewer HorizontalScrollBarVisibility="Auto" 
                      VerticalScrollBarVisibility="Auto" 
                      Grid.Column="1" Grid.Row="1">
            <StackPanel x:Name="innerPanel" Grid.Column="1" 
                        Grid.Row="1" Orientation="Vertical">
            </StackPanel>
        </ScrollViewer>

还有我的工具栏代码:

   <ToolBarTray Grid.Row="1" Grid.ColumnSpan="2" Background="LightGray" >
        <ToolBar x:Name="toolbar" Background="LightGray" ToolBarTray.IsLocked="False">
            <Button Command="New" Content="New" />
            <Button Command="Open" Content="Open" />
            <Button Command="Save" Content="Save" />
            <Button Command="Cut" Content="Cut" />
            <Button Command="Copy" Content="Copy" />
            <Button Command="Paste" Content="Paste" />
            <Button Command="Record" Content="Record" />
            <Button Command="Play" Content="Play" />
            <Button Command="Pause" Content="Pause" />
            <Button Command="Stop" Content="Stop" />
            <Image Width="20" Margin="5,0"  Source="C:\VolumeIcon1.png"/>
            <Slider x:Name="VolumeControl" Maximum="100" Width="250"  
                    TickPlacement="BottomRight" Foreground="DarkGray" 
                    TickFrequency="1" IsSnapToTickEnabled="True"/>
            <TextBox Text="{Binding ElementName=VolumeControl, Path=Value, 
                UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" 
                     TextAlignment="Right" Width="30" 
        </ToolBar>
    </ToolBarTray>

以下是一些按钮的图片,分别表示它们处于禁用状态或启用状态。

enter image description here enter image description here

编辑: 这四个文件应重现我遇到的问题的基本版本。单击Scrollviewer区域后,工具栏中的新按钮将被禁用并显示为灰色。

MainWindow.xaml

<Window x:Class="UIMilestone.MainWindow" x:Name="window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:UIMilestone"
    mc:Ignorable="d"
    SizeChanged="OnWindowSizeChanged"
    Title="MainWindow" Height="450" Width="800" Background="LightGray">
<Grid x:Name="mainGrid" ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="5"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="5"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition x:Name="row0" Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="20"/>
    </Grid.RowDefinitions>
    <ScrollViewer     HorizontalScrollBarVisibility="Auto" 
                      VerticalScrollBarVisibility="Auto" 
                      Grid.Column="1" Grid.Row="1">
    </ScrollViewer>
</Grid>

MainWindow.xaml.cs

namespace UIMilestone
{
    public partial class MainWindow : Window
    {
        //Links C# project to methods in c++

        public MainWindow()
        {
            InitializeComponent();
            mainGrid.Measure(new Size(800, 450));
            mainGrid.Arrange(new Rect(0, 0, 800, 450));

            TopToolbars tools = new TopToolbars(this);
            mainGrid.Children.Add(tools);

        }

        protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
        }
    }
}

TopToolBars.xaml

<UserControl x:Class="UIMilestone.TopToolbars"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:UIMilestone"
         mc:Ignorable="d" 
         d:DesignHeight="47" d:DesignWidth="790">
<UserControl.CommandBindings>
    <CommandBinding Command="New" CanExecute="New_CanExecute" />
</UserControl.CommandBindings>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="327.333"/>
        <ColumnDefinition Width="462.667"/>
    </Grid.ColumnDefinitions>

    <ToolBar x:Name="toolbar" Background="LightGray" ToolBarTray.IsLocked="False">
        <Button Command="New" Content="New" />
    </ToolBar>
</Grid>

TopToolBars.xaml.cs

using System.Windows.Controls;
using System.Windows.Input;

namespace UIMilestone
{
public partial class TopToolbars : UserControl
{
    private MainWindow win;

    public TopToolbars(MainWindow mainWindowInst)
    {
        win = mainWindowInst;
        InitializeComponent();
        this.SetValue(Grid.RowProperty, 0);
        this.SetValue(Grid.ColumnProperty, 1);
    }

    private void New_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}

}

1 个答案:

答案 0 :(得分:0)

这是因为路由命令从具有焦点的元素开始其行程,使可视化树冒泡。但是,您的工具栏元素不在innerPanel的父链中。

选项1:将工具栏元素明确指定为命令目标:将CommandTarget =“ {Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = local:TopToolbar}}”“添加到按钮中

选项2:将命令绑定放在MainWindow中。

选项3:使用MVVM:在视图模型中创建ICommand属性,并在按钮中创建Command =“ {Binding MyCommand}”