【问题标题】:map shape to another button将形状映射到另一个按钮
【发布时间】:2017-08-28 07:47:14
【问题描述】:
<Path Data="M0.5,0 L0.5,1 M0,0.5 L1,0.5"
      StrokeThickness="4"
      Stretch="Fill"
      Stroke="Blue" />

如果按钮是窗口中唯一的东西,这将在按钮上绘制一个加号, 但是如何才能映射到这个按钮的路径呢?

<ControlTemplate x:Key="BT_SIGN" TargetType="{x:Type Button}">

        <Grid Cursor="None" Margin="0,0,1.333,0">
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF1E8CF3" Offset="1"/>
                </LinearGradientBrush>

            </Grid.Background>
        </Grid>
    </ControlTemplate>
<Button           
        Grid.Column="3" 
        HorizontalAlignment="Left" 
        Height="44.8" 
        Width="75.2" 
        Margin="22.32,24.6,0,0" 
        VerticalAlignment="Top"
        Template="{DynamicResource BT_SIGN}">
    </Button>

【问题讨论】:

    标签: c# wpf xaml path


    【解决方案1】:

    试试这个,即将&lt;ContentPresenter /&gt; 添加到模板并将Content 属性设置为您的Path

    <ControlTemplate x:Key="BT_SIGN" TargetType="{x:Type Button}">
        <Grid Cursor="None" Margin="0,0,1.333,0">
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF1E8CF3" Offset="1"/>
                </LinearGradientBrush>
            </Grid.Background>
            <ContentPresenter />
        </Grid>
    </ControlTemplate>
    
    <Button           
        Grid.Column="3" 
        HorizontalAlignment="Left" 
        Height="44.8" 
        Width="75.2" 
        Margin="22.32,24.6,0,0" 
        VerticalAlignment="Top"
        Template="{DynamicResource BT_SIGN}">
            <Path Data="M0.5,0 L0.5,1 M0,0.5 L1,0.5"
                StrokeThickness="4"
                Stretch="Fill"
                Stroke="Blue" />
    </Button>
    

    【讨论】:

      【解决方案2】:

      只需在按钮标记内设置该特定按钮的路径即可。

      <Button           
          Grid.Column="3" 
          HorizontalAlignment="Left" 
          Height="44.8" 
          Width="75.2" 
          Margin="22.32,24.6,0,0" 
          VerticalAlignment="Top"
          Template="{DynamicResource BT_SIGN}">
      
          <Path Data="M0.5,0 L0.5,1 M0,0.5 L1,0.5"
        StrokeThickness="4"
        Stretch="Fill"
        Stroke="Blue" />
      
      </Button>
      

      【讨论】:

      • 是的,我做到了。但是由于我没有您的模板,因此我没有为按钮设置任何模板。您可以将代码发布到您的模板吗?
      • 看mm8的回答。您的模板中缺少
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      相关资源
      最近更新 更多