如何解决ToggleButton /矩形和网格行距问题

时间:2019-09-04 02:49:55

标签: c# wpf

我正在尝试在网格中创建一个按钮,当我单击该按钮时,它将增加网格的行距。我创建了一个Rectangle放置在ToggleButton的后面,以便在悬停时更好看。单击图像/切换按钮时,行跨度可以完美切换。但是,如果在ToggleButton之前单击矩形,则仅适用于该矩形。单击一次按钮后,矩形单击将不再起作用。

<Grid ShowGridLines="False" Background="#282828">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
            <RowDefinition Height="100" />
            <RowDefinition Height="68" />
        </Grid.RowDefinitions>

        <Rectangle Grid.Column="0" Grid.RowSpan="1000">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#1c1c1c" Offset="0" />
                    <GradientStop Color="#1c1c1c" Offset="1" />
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>

        <Rectangle x:Name="MenuButton" MouseEnter="Menu_Mouse_Enter" Grid.Column="0" Grid.Row="0" MouseLeave="Menu_Mouse_Leave" MouseDown="Menu_Button_Click">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#1c1c1c" Offset="0" />
                    <GradientStop Color="#1c1c1c" Offset="1" />
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>

        <ToggleButton x:Name="MenuToggleButton" MouseEnter="Menu_Mouse_Enter" MouseLeave="Menu_Mouse_Leave" Click="Menu_Button_Click" Grid.Row="0" Grid.Column="0" Height="32" Width="32">
            <ToggleButton.Template>
                <ControlTemplate>
                    <Image Width="32" Height="32" Source="Resources/menu.png" />
                </ControlTemplate>
            </ToggleButton.Template>
        </ToggleButton>
    </Grid>
public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Title = "Youtube+ | #1 Youtube Client";
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Height = 768;
            this.Width = 1024;
        }

        private void Menu_Button_Click(object sender, RoutedEventArgs e)
        {
            var element = (UIElement) MenuButton;
            if (MenuToggleButton.IsChecked == true)
            {
                Grid.SetRowSpan(element, 1);

            }
            else
            {
                Grid.SetRowSpan(element, 1000);

            }
            Console.WriteLine("Sent");
        }

        private void Menu_Mouse_Leave(object sender, MouseEventArgs e)
        {
            var element = (UIElement) e.Source;

            var c = Grid.GetColumn(element);
            var r = Grid.GetRow(element);
            if (c == 0 || r == 0)
            {
                Brush darker = new SolidColorBrush(Color.FromRgb(28,28,28));
                MenuButton.Fill = darker;
            }
            Console.WriteLine("Hello");
            Console.WriteLine(r);

        }

        private void Menu_Mouse_Enter(object sender, MouseEventArgs e)
        {
            var element = (UIElement) e.Source;

            var c = Grid.GetColumn(element);
            var r = Grid.GetRow(element);
            if (c == 0 && r == 0)
            {
                Brush dark = new SolidColorBrush(Color.FromRgb(40,40,40));
                MenuButton.Fill = dark;
            }
        }
    }

这应该是使它起作用的部分,对吧?

private void Menu_Button_Click(object sender, RoutedEventArgs e)
        {
            var element = (UIElement) MenuButton;
            if (MenuToggleButton.IsChecked == true)
            {
                Grid.SetRowSpan(element, 1);
            }
            else
            {
                Grid.SetRowSpan(element, 1000);

            }
            Console.WriteLine("Sent");
        }

我的Console.WriteLine(“ Sent”);也一文不值不会显示在控制台中。

1 个答案:

答案 0 :(得分:0)

不确定,但是最好不要将矩形悬停在矩形上进行扩展,而是编辑“切换”的“样式和控件模板”。打开切换按钮的模板后,请根据需要更改外观。您还可以创建一些TemplateBinding或自定义控件,在其中绑定数据绑定或元素绑定时的属性。

我希望我在这里确实有意义。