2012-05-29 126 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); 

希望它的幫助。