2012-11-18 59 views
1

如果问题不一定有意义,我很抱歉,但我是编程新手。 O_0XAML,C# - 绑定到用户创建控件的属性

我创建在Blend用户定义XAML控制充当按钮:

<Style x:Key="GeekMDCalc_Button_Std." TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Canvas x:Name="canvas" Width="200" Height="200" Background="#FFA09F9F"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualStateGroup.Transitions> 
             <VisualTransition GeneratedDuration="0:0:0.15"/> 
            </VisualStateGroup.Transitions> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <ColorAnimation Duration="0" To="#FF0CFF00" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/> 
              <ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="canvas" d:IsOptimized="True"/> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"/> 
            <VisualState x:Name="PointerOver"> 
             <Storyboard> 
              <ColorAnimation Duration="0" To="#FF0CFF00" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="FocusStates"> 
            <VisualState x:Name="Focused"/> 
            <VisualState x:Name="PointerFocused"/> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Rectangle x:Name="rectangle" Fill="#FFF4F4F5" Height="57" Width="200" Canvas.Top="143"/> 
          <ContentPresenter Height="57" Canvas.Top="143" Width="190" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="Black" Canvas.Left="10"/> 
          <Image x:Name="image" Height="128" Canvas.Left="10" Canvas.Top="10" Width="180"/> 
         </Canvas> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

我然后我逐个添加一个GridView的内部按钮:

<GridView Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" FontFamily="Global User Interface"> 
     <Button Content="Emergency" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Intensive Care" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Internal Med." Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Surg/Trauma" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Renal" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Electrolytes" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Cardiology" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Pediatrics" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="Neurology" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="GI" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
     <Button Content="General" Style="{StaticResource GeekMDCalc_Button_Std.}" FontFamily="DilleniaUPC" FontSize="45.33" FontWeight="Light"/> 
    </GridView> 

I” m试图通过将图像绑定到我创建的控件模板中的图像容器来应用背景,但我没有任何运气。我希望每个按钮都有独立的背景,我希望在我的代码背后定义这些背景。我试过了几个小时,但似乎找不到方法来轻松更改我创建的按钮控件的图像源。

感谢您的帮助,并与一个新手^ _^

回答

0

你并不需要在风格的图像,我也会把它不好的做法耐心。一个Button是一个ContentPresenter,所以它的主要目的是“呈现内容”,Content本身可以是任何东西;另一个控件,图像一个字符串或一个自定义类。因此,您可以直接将图像分配给按钮,而无需在模板中添加图像对象。如果您的Content不是WPF控件,WPF需要一些关于如何显示Content的指导。因此,您需要告诉他您希望内容的外观如何,并且这是通过模板精确完成的,即DataTemplate。如果你只是需要一个图标,这应该是足矣

<Button> 
    <Image Source="SomeIcon"/> 
</Button> 

如果您需要更多显示的内容,你需要定义一个DataTemplate,我会建议你使用一些数据绑定用它来充分发挥其潜在。这是一个简短的例子:

class MyButtonInfo 
{ 
    public ImageSource Icon{get;set;} 
    public string Caption{get;set;} 
    public Command Command{get;set;} 
} 

<DataTemplate x:Key="MyTemplate"> 
    <StackPanel> 
     <Image Source="{Binding Icon}"/> 
     <TextBlock Text="{Binding Caption}"/> 
    </StackPanel> 
</DataTemplate> 

<Button Content="{Binding ButtonInfo}" ContentTemplate="{StaticResource MyTemplate}" Command="{Binding Command}"> 
</Button> 
+0

非常感谢!我会玩这个,看看我能否得到它的工作。 – trn450

+0

稍微调整一下,你为我概述的概念清除了一切!非常感谢! – trn450

相关问题