2012-05-29 3 views
1

У меня проблема с пользовательским элементом управления и его шаблоном.Windows Phone Template and Button

У меня есть элемент управления, который расширяет Button, его шаблон был создан с помощью Expression Blend 4, и он доступен в ressources 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); 

Надежда свою помощь.