2012-04-19 18 views
0

私はWinRTに対する最初のカスタムユーザーコントロールを作成していますが、問題が発生しました。Windows 8 Metroでのカスタムコントロールの作成

私はPART_NwBadgeというイメージを公開したいと思います。私のコントロールでは、それは依存関係のプロパティとして表示されます。それから私はスタイルのセッターを通してデフォルト値を提供したいと思います。この部分は機能しません。代わりに、(BadgedButton.csの)DependencyPropertyのデフォルト値が適用されています。

私が説明したことを行うことは可能ですか?または、C#コードでデフォルト値を設定する必要がありますか? C#コードで値を設定する必要がある場合は、誰かがコード内で画像リソースを読み込む方法についてコメントしますか?多くの検索の後、私はまだ解決した解決策を見つけていません。

最後に、これはカスタムコントロールを作成するための私の最初の重大な試みであるため、問題に直接関係していなくても改善できる点をご提案ください。

のWindows 8コンシューマープレビュー
C#/ WinRTの/メトロ
のVisual Studio 11ベータ

テーマ/ Generic.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:l="using:InkSdkTestApplication.Controls"> 

    <Style TargetType="l:BadgedButton"> 
     <Setter Property="Width" Value="36"/> 
     <Setter Property="Height" Value="36"/> 
     <Setter Property="Background" Value="#1C1C1C"/> 
     <Setter Property="BorderBrush" Value="White"/> 
     <Setter Property="BorderThickness" Value="2"/> 
     <Setter Property="NwBadge"> 
      <Setter.Value> 
       <Image Width="16" Height="16" Source="../Assets/mouse_16x16.png"/>    
      </Setter.Value> 
     </Setter> 
     <Setter Property="NwBadgeVisibility" Value="Collapsed"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="l:BadgedButton"> 
        <Border x:Name="PART_Border" 
          Width="{TemplateBinding Width}" 
          Height="{TemplateBinding Height}" 
          Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}"> 

         <Grid> 
          <ContentPresenter x:Name="PART_Content" 
               HorizontalAlignment="Center" 
               VerticalAlignment="Center" 
               Content="{TemplateBinding Content}"/> 

          <Image x:Name="PART_NwBadge" 
            HorizontalAlignment="Left" 
            VerticalAlignment="Top" 
            Width="16" Height="16" 
            Visibility="{TemplateBinding NwBadgeVisibility}" 
            Source="{TemplateBinding NwBadge}"/> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

コントロール/ BadgedButton.cs

namespace InkSdkTestApplication.Controls 
{ 
    public sealed class BadgedButton : Control 
    { 
     #region // Dependency Properties 
     public static DependencyProperty ContentProperty = 
      DependencyProperty.Register(
       "Content", 
       typeof(FrameworkElement), 
       typeof(BadgedButton), 
       new PropertyMetadata(null)); 

     public static DependencyProperty NwBadgeProperty = 
      DependencyProperty.Register(
       "NwBadge", 
       typeof(Image), 
       typeof(BadgedButton), 
       new PropertyMetadata(null)); 

     public static DependencyProperty NwBadgeVisibilityProperty = 
      DependencyProperty.Register(
       "NwBadgeVisibility", 
       typeof(Visibility), 
       typeof(BadgedButton), 
       new PropertyMetadata(Visibility.Visible)); 
     #endregion 

     #region // Public Properties 
     public FrameworkElement Content 
     { 
      get { return (FrameworkElement)GetValue(ContentProperty); } 
      set { SetValue(ContentProperty, value); } 
     } 

     public Image NwBadge 
     { 
      get { return (Image)GetValue(NwBadgeProperty); } 
      set { SetValue(NwBadgeProperty, value); } 
     } 

     public Visibility NwBadgeVisibility 
     { 
      get { return (Visibility)GetValue(NwBadgeVisibilityProperty); } 
      set { SetValue(NwBadgeVisibilityProperty, value); } 
     } 
     #endregion 

     public BadgedButton() 
     { 
      this.DefaultStyleKey = typeof(BadgedButton); 
     } 
    } 
} 
+0

カスタムコントロールを作成しないことをおすすめできますか?バインディングで通常のテンプレートを作成するだけですか? –

答えて

関連する問題