【问题标题】:responsive style menu button in WPFWPF中的响应式菜单按钮
【发布时间】:2015-10-06 11:28:06
【问题描述】:

希望有人能提供帮助,我是 WPF 的新手,想创建一个类似于移动应用程序和响应式 Web 应用程序中的菜单按钮的按钮,它是一个带有三条水平线的方形按钮。

我尝试创建一个带有画布和三行的按钮,但这不能正常工作。

谁能推荐可以实现此目的的 XAML 吗?

编辑

我已将答案中的代码添加到我的应用程序中,XAML 如下

<Button x:Name="systemButton" IsTabStop="False" Style="{StaticResource LightWindowButtonStyle}" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Button.Content>
        <Grid Width="31" Height="23" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Path Data="M8,8 L28,8" Fill="{TemplateBinding Foreground}" Height="4" StrokeThickness="4" Stretch="Fill" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Center" />
            <Path Data="M8,8 L28,8" Fill="{TemplateBinding Foreground}" Height="4" StrokeThickness="4" Stretch="Fill" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Center" Grid.Row="1" />
            <Path Data="M8,8 L28,8" Fill="{TemplateBinding Foreground}" Height="4" StrokeThickness="4" Stretch="Fill" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Center" Grid.Row="2" />
        </Grid>
    </Button.Content>
</Button>

在我的AeroWindow 类中,我正在获取按钮的实例并绑定到单击事件,如下所示

var systemButton = this.GetTemplateChild("systemButton") as Button;
if (systemButton != null)
{
    systemButton.Click += this.SystemButtonOnClick;
}

但是当我单击按钮时,事件处理程序永远不会被触发。我检查了systemButton 不是null,因此Click 事件被绑定到事件处理程序,事件处理程序上的断点永远不会被击中。有人有什么想法吗?

【问题讨论】:

    标签: c# wpf xaml responsive-design


    【解决方案1】:

    您需要将内容放在 Button 中,并为此应用内容模板。

    <Window.Resources>  
    
    
        <DataTemplate x:Key="DataTemplate1">
            <Grid Width="51" Height="42">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="1"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="2"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="3"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="4"/>
            </Grid>
        </DataTemplate> 
    
    
    </Window.Resources>
    
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <Button Content="" HorizontalAlignment="Left" Margin="112,88,0,0" VerticalAlignment="Top" Width="56" Height="48"
         ContentTemplate="{DynamicResource DataTemplate1}"/>
        <Button HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="0,88,232,0" VerticalAlignment="Top" Width="67" Height="56">
    
            <Button.Content>
                <Grid Width="51" Height="42">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29"  Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" StrokeThickness="5"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29"  Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="1" StrokeThickness="5"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29"  Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="2" StrokeThickness="5"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29"  Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="3" StrokeThickness="5"/>
                <Path Data="M0,5 L51,5" Fill="#FF2DBE29"  Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="4" StrokeThickness="5"/>
            </Grid>
            </Button.Content>           
            </Button>
    

    我已经更新了我的答案。在 DataTemplate 中,我们使用 Height,而在下一个 Button 中,我们仅使用 StrokeThickness。

    对于使用样式,您可以进行以下更改:

    <Window.Resources>  
    
            <DataTemplate x:Key="DataTemplate1">
                <Grid Width="51" Height="42">
                    <Grid.Resources>
                        <SolidColorBrush x:Key="PathFillBrush" Color="#FF2DBE29"/>
                    </Grid.Resources>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Path Data="M0,5 L51,5" Fill="{DynamicResource PathFillBrush}" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center"/>
                    <Path Data="M0,5 L51,5" Fill="{DynamicResource PathFillBrush}" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="1"/>
                    <Path Data="M0,5 L51,5" Fill="{DynamicResource PathFillBrush}" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="2"/>
                    <Path Data="M0,5 L51,5" Fill="{DynamicResource PathFillBrush}" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="3"/>
                    <Path Data="M0,5 L51,5" Fill="{DynamicResource PathFillBrush}" Height="3" Stretch="Fill" Stroke="#FF2DC65A" VerticalAlignment="Center" Grid.Row="4"/>
                </Grid>
            </DataTemplate>         
    
            <Style TargetType="Button">
                <Setter Property="ContentTemplate" Value="{DynamicResource DataTemplate1}"/>
            </Style>
    
        </Window.Resources>
    

    【讨论】:

    • 谢谢你,我的应用程序中的线条很细(如 1px),怎么可能改变它,我认为高度是正确的?还如何将 Fill 和 Stroke 绑定到模板中指定的值。另外,我将如何在 中应用相同的内容,抱歉有一百万个问题
    • @Neil 提出单独的问题,因为还有其他人也在使用这个网站。这是一个很好的做法。无论如何,对于细线:在上面的示例中使用 StrokeThickness=5 将使线条足够好并完全删除高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2021-05-25
    • 2013-02-20
    相关资源
    最近更新 更多