WPF C#Scrollviewer正在禁用工具栏按钮 [英] WPF C# Scrollviewer is disabling toolbar buttons

查看:76
本文介绍了WPF C#Scrollviewer正在禁用工具栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个C#WPF应用程序。我在网格单元格中放置了一个Scrollviewer。当应用程序运行时,如果我单击Scrollviewer
单元格内的任何位置,我的所有工具栏按钮都会突然被禁用,并且不会重新启用。我的工具栏由其自己的类管理,并且位于与Scrollviewer不同的单元格中。我已经设置了命令绑定并设置了除"暂停"之外的所有按钮。
即可启用。我想知道这个问题是否与Scrollviewer方法BringToFront()有关。


我该如何解决这个问题?

How can I fix this?


感谢您的帮助。

Thanks for any help.


这是我的ScrollViewer代码:

Here is my ScrollViewer code:

 <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>

我的ToolBar代码:

   <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>








推荐答案

stackpanel位于scrollviewer而不是网格中

The stackpanel is inside a scrollviewer rather than a grid

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

Grid.Row和Grid.Column在那里不会做太多。

Grid.Row and Grid.Column aren't going to do much there.

在你的标记中,stackpanel和toolbartray看起来都像是进入网格的同一个单元格。

In your markup both the stackpanel and toolbartray look like they're going into the same cell of a grid.

但我认为你的问题可能就是你的命令。

But I think your problem is likely to be your commands.

我的测试标记是:

    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ToolBarTray Background="LightGray" >
            <ToolBar x:Name="toolbar" Background="LightGray" ToolBarTray.IsLocked="False">
                <Button Command="{Binding TestCommand}" Content="Test" />
            </ToolBar>
        </ToolBarTray>
        <ScrollViewer Grid.Row="1">
            <StackPanel>
               <TextBox Text="SomeText"/>
            </StackPanel>
        </ScrollViewer>
    </Grid>

我正在使用mvvmlight作为我的testcommand:

I'm using mvvmlight for my testcommand:

using GalaSoft.MvvmLight.CommandWpf;

namespace wpf_12
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private RelayCommand testCommand;
        public RelayCommand TestCommand
        {
            get
            {
                return testCommand
                ?? (testCommand = new RelayCommand(
                 () =>
                 {
                     MessageBox.Show("It worked !");
                 }));
            }
        }




我的测试按钮已启用黑色文本。

And my Test button has black enabled text.

单击文本框,它仍然启用。

Click on the textbox and it still is enabled.

单击按钮,我会收到一个消息框。

Click the button and I get a message box.

如果我将我的按钮改为:

If I instead change my button to:

<Button Command="New" Content="New" />

然后我可以看到该按钮已被禁用,因为我没有给它一个新命令。

Then I can see the button is disabled because I've not given it a new command.

我不知道我知道你有什么其他代码或者你是如何尝试连接命令的,但这是我要研究的第一件事。

I don't know what other code you have or how you are trying to hook up a command but that's the first thing I'd look into.


这篇关于WPF C#Scrollviewer正在禁用工具栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆