2012-05-29 46 views
1

我有一个自定义控件及其模板的问题。Windows Phone模板和按钮

我有一个扩展Button的控件,它的模板是通过Expression Blend 4创建的,它可以在App.xaml的资源中使用。

这是模板:

<ControlTemplate x:Key="ImageButtonControlTemplate1" TargetType="maquette_v0__1_Controles_persos:ImageButton"> 
     <Grid Margin="0" Width="150"> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="CommonStates"> 
        <VisualState x:Name="Disabled"/> 
        <VisualState x:Name="Normal"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image"> 
           <DiscreteObjectKeyFrame KeyTime="0"> 
            <DiscreteObjectKeyFrame.Value> 
             <Visibility>Collapsed</Visibility> 
            </DiscreteObjectKeyFrame.Value> 
           </DiscreteObjectKeyFrame> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="MouseOver"/> 
        <VisualState x:Name="Pressed"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image1"> 
           <DiscreteObjectKeyFrame KeyTime="0"> 
            <DiscreteObjectKeyFrame.Value> 
             <Visibility>Collapsed</Visibility> 
            </DiscreteObjectKeyFrame.Value> 
           </DiscreteObjectKeyFrame> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
      <Image x:Name="image1" Source="{TemplateBinding Image}" Height="150"/> 
      <Image x:Name="image" Source="{TemplateBinding PressedImage}" Height="150"/> 
     </Grid> 
    </ControlTemplate> 

在一个页面上,我要动态地添加自定义的控制。 要做到这一点,我在我的文件Xaml“menu_commune”中有一个网格,它在这个网格中,我想显示我的控制。

这是代码C#:

ImageButton b = new ImageButton(); 
b.SetValue(Grid.RowProperty, 0); 
b.SetValue(Grid.ColumnProperty, 2); 
// bool t = Application.Current.Resources["ImageButtonControlTemplate1"] == null; 
ControlTemplate ctemp = (ControlTemplate) Application.Current.Resources["ImageButtonControlTemplate1"]; 
b.Template = ctemp; 
BitmapImage bi_noPress = new BitmapImage(new Uri("/Images/menu_agenda.png")); 
BitmapImage bi_Press = new BitmapImage(new Uri("/Images/menu_agenda_selected.png")); 
b.Image = bi_noPress; 
b.PressedImage = bi_Press; 
menu_commune.Children.Add(b); 

的问题是什么也看不见了。 但是,Xaml中的声明运行良好。

<UC:ImageButton Grid.Row="2" Grid.Column="2" Image="../Images/menu_office-de-tourisme.png" PressedImage="../Images/menu_office-de-tourisme_selected.png" Height="150" Template="{StaticResource ImageButtonControlTemplate1}" Click="tourisme_Click" /> 

感谢

回答

0
<UC:ImageButton Grid.Row="2" Grid.Column="2" Image="../Images/menu_office-de-tourisme.png" PressedImage="../Images/menu_office-de-tourisme_selected.png" Height="150" Template="{StaticResource ImageButtonControlTemplate1}" Click="tourisme_Click" /> 

在这里,您使用Grid.Row="2" Grid.Column="2",并在代码中你需要使用相同的值。更换

b.SetValue(Grid.RowProperty, 0); 
b.SetValue(Grid.ColumnProperty, 2); 

b.SetValue(Grid.RowProperty, 2); 
b.SetValue(Grid.ColumnProperty, 2); 

希望它的帮助。