2010-01-20 24 views
5

我有我想要在我的Silverlight应用程序中的UserControl上的多个按钮中使用的按钮内容。这里是一个按钮的代码:XAML中的重用按钮内容

<Grid x:Name="LayoutRoot" Background="White"> 

    <Button Grid.Column="1" IsEnabled="{Binding PrivilegeChanged}" Height="24" Width="24"> 
     <Button.Content> 
      <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       x:Name="UndoIcon" Width="16" Height="16" Clip="F1 M 0,0L 16,0L 16,16L 0,16L 0,0" UseLayoutRounding="False"> 
       <Canvas x:Name="Arrow_2" Width="16" Height="16" Canvas.Left="0" Canvas.Top="0"> 
        <Path Width="17.0154" Height="17" Canvas.Left="-0.5" Canvas.Top="-0.499999" Stretch="Fill" 
         StrokeLineJoin="Round" Stroke="#FF006432" Fill="#FF00C800" 
         Data="F1 M 12.5819,16C 14.1685,12.7951 14.1052,6.14911 11.0969,4.25C 9.23816,3.07665 6.71915,3.4789 5.40404,5.25L 8.12669,8.25L 0,8.91667L 0,9.53674e-007L 3.17642,3.25C 4.16648,1.91667 5.52584,0.61155 7.13664,0.25C 9.85332,-0.359774 13.4395,0.629333 15.0571,2.91667C 17.402,6.23256 15.0026,12.7401 12.5819,16"/> 
       </Canvas> 
      </Canvas> 
     </Button.Content> 
    </Button> 

</Grid> 

我该如何使Button.Content可重用而不删除按钮轮廓?

+0

你可以请添加一个说明你为什么要这样做。你的问题的简短答案是你不能这样做,因为一个UIElement(例如一个Canvas)只能有一个Parent(即一个Canvas一次只能在一个按钮中)。所以你可以为你的按钮创建一个自定义的ControlTemplate,或者你可以创建一个自定义的MyButton类型。如果你扩展你的目标,可以适当地给你提供建议。 – KeithMahoney

回答

4

最简单的办法是坚持你的按钮设计内容在自己的UserControl : -

<UserControl x:Class="SilverlightApplication1.MyButton" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 
    <Canvas Width="16" Height="16" Clip="F1 M 0,0L 16,0L 16,16L 0,16L 0,0" UseLayoutRounding="False"> 
     <Canvas Width="16" Height="16" Canvas.Left="0" Canvas.Top="0"> 
      <Path Width="17.0154" Height="17" Canvas.Left="-0.5" Canvas.Top="-0.499999" Stretch="Fill" 
         StrokeLineJoin="Round" Stroke="#FF006432" Fill="#FF00C800" 
         Data="F1 M 12.5819,16C 14.1685,12.7951 14.1052,6.14911 11.0969,4.25C 9.23816,3.07665 6.71915,3.4789 5.40404,5.25L 8.12669,8.25L 0,8.91667L 0,9.53674e-007L 3.17642,3.25C 4.16648,1.91667 5.52584,0.61155 7.13664,0.25C 9.85332,-0.359774 13.4395,0.629333 15.0571,2.91667C 17.402,6.23256 15.0026,12.7401 12.5819,16"/> 
     </Canvas> 
    </Canvas> 
</UserControl> 

现在您可以创建该用户的多个实例控制哪里你需要这个内容: -

<UserControl x:Class="SilverlightApplication1.Test" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:SilverlightApplication1" 
    Width="400" Height="300"> 
    <Grid x:Name="LayoutRoot" Background="White"> 
     <StackPanel Orientation="Horizontal"> 
      <Button Grid.Column="1" IsEnabled="{Binding PrivilegeChanged}" Height="24" Width="24" Margin="2"> 
       <local:MyButton /> 
      </Button> 
      <Button Grid.Column="1" IsEnabled="{Binding SomethingElseChanged}" Height="24" Width="24" Margin="2"> 
       <local:MyButton /> 
      </Button> 
     </StackPanel> 

    </Grid> 
</UserControl> 
0

通常情况下,您将创建一个Template,为您的按钮创建自定义布局。更快更直接的解决方案是将内容设置为Style,然后将样式应用于所需的按钮。

定义样式与内容:

<Window.Resources> 
    <Style x:Key="ButtonArrowStyle" TargetType="Button"> 
     <Setter Property="Content"> 
      <Setter.Value> 
       <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Name="UndoIcon" Width="16" Height="16" Clip="F1 M 0,0L 16,0L 16,16L 0,16L 0,0" UseLayoutRounding="False"> 
        <Canvas x:Name="Arrow_2" Width="16" Height="16" Canvas.Left="0" Canvas.Top="0"> 
         <Path Width="17.0154" Height="17" Canvas.Left="-0.5" Canvas.Top="-0.499999" Stretch="Fill" 
        StrokeLineJoin="Round" Stroke="#FF006432" Fill="#FF00C800" 
        Data="F1 M 12.5819,16C 14.1685,12.7951 14.1052,6.14911 11.0969,4.25C 9.23816,3.07665 6.71915,3.4789 5.40404,5.25L 8.12669,8.25L 0,8.91667L 0,9.53674e-007L 3.17642,3.25C 4.16648,1.91667 5.52584,0.61155 7.13664,0.25C 9.85332,-0.359774 13.4395,0.629333 15.0571,2.91667C 17.402,6.23256 15.0026,12.7401 12.5819,16"/> 
        </Canvas> 
       </Canvas> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

,然后定义按钮使用的样式:

<Button Style="{StaticResource ButtonArrowStyle}"/> 
+0

我试过这个,但收到错误信息:“价值不在预期的范围内。”在Style =“{StaticResource ButtonArrowStyle}”上。 –

+0

@Rachel Martin:我在测试应用程序中完全使用了上面的内容,并且它工作正常。你确定没有可能超出范围的其他财产吗?尝试只复制粘贴上面的内容(没有固定页边距或更改值),并查看它是否可以先编译。 –

+0

好吧,我完全复制了Style并将其放置在Grid.Resources中。我目前在Microsoft Visual Studio 2008和Microsoft Expression Blend 3中使用Silverlight 3,使用Visual Studio,我在运行时收到错误消息。使用Blend,我在编译时得到错误信息,但它仍然会编译并在运行时给我一个AG_E_INVALID_ARGUMENT错误。 –