2016-08-09 24 views
0

私のアプリケーション用のカスタムボタンを作成しました。コンテンツにはTextblockSymbolIconがあり、両方ともGridに配置されています。 これは非常に単純なコードカスタムUserControlの読み込み中にエラーが発生しました

<UserControl 
    ...> 

     <Grid> 
      <Button Name="ButtonControl"> 
       <Button.Content> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition /> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions> 

         <SymbolIcon Grid.Column="0" 
            Symbol="Italic" 
            Name="SymbolIconColumn" 
            /> 
         <TextBlock Grid.Column="1" 
            Text="" 
            Name="TextBlockColumn" 
            /> 
        </Grid> 
       </Button.Content> 
      </Button> 
     </Grid> 

</UserControl> 

され、そのクラスファイルで、私はプロパティのカップルを宣言した:

  1. SymbolIcon基本的に SymbolIcon
  2. TextセットのSymbolプロパティが設定されますTextのプロパティTextblock
  3. Click私が私を考える

    public static readonly DependencyProperty SymbolIconProperty = 
          DependencyProperty.Register("SymbolIcon", typeof(Symbol), 
           typeof(TimerButton), new PropertyMetadata(Symbol.Italic)); 
    
    public static readonly DependencyProperty TextProperty = 
          DependencyProperty.Register("Text", typeof(string), 
           typeof(TimerButton), new PropertyMetadata("")); 
    
    public static readonly DependencyProperty ClickProperty = 
          DependencyProperty.Register("Click", typeof(RoutedEventHandler), 
           typeof(TimerButton), new PropertyMetadata(null)); 
    

    public string Text 
    { 
        get 
        { 
         return (string)GetValue(TextProperty); 
        } 
        set 
        { 
         SetValue(TextProperty, value); 
         TextBlockColumn.Text = value; 
        } 
    } 
    
    public Symbol SymbolIcon 
    { 
        get 
        { 
         return (Symbol)GetValue(SymbolIconProperty); 
        } 
        set 
        { 
         SetValue(SymbolIconProperty, value); 
        } 
    } 
    
    
    public RoutedEventHandler Click 
    { 
        get 
        { 
         return (RoutedEventHandler)GetValue(ClickProperty); 
        } 
        set 
        { 
         SetValue(ClickProperty, value); 
         ButtonControl.Click += value; 
        } 
    } 
    
    public RoutedEventHandler Click 
    { 
        get 
        { 
         return (RoutedEventHandler)GetValue(ClickProperty); 
        } 
        set 
        { 
         SetValue(ClickProperty, value); 
         ButtonControl.Click += value; 
        } 
    } 
    

    私は彼らのDependencyProperty.Register機能と、それらのすべてを登録しました:Button

このため電子Clickイベントハンドラは、背後にあるコードでありますすべてをやったよね? だから、私は私のページに名前空間を追加し、負荷にそれをしようとした場合、この

<Page 
    ... 
    xmlns:bt="using:MyProject.Resources"...> 

<bt:TimerButton 
       SymbolIcon="Accept" 
       Text="Accept" 
       /> 

のようなデザイナーでもロードされません、それは私にこの

System.Runtime.Remotingを与えます.RemotingException [9652]デザイナープロセスが予期せず終了しました!

また、ページが読み込まれると、デバッグがクラッシュします。

+0

UWPやWPFとプロパティを結合することによって、これをやりましたか?それは両方になることはできません。 – Clemens

+0

"私はすべてをやったと思いますよね?"ではない正確に。依存プロパティーのCLRラッパーのsetterメソッドに 'SetValue'以外のものを持つべきではありません。 'TextBlockColumn.Text = value;'を呼び出さないでください。代わりに、TextBlockのTextプロパティをUserControlのTextプロパティにバインドする必要があります。 SymbolIconと同じです。 – Clemens

+0

ありがとうございます。それはUWPです。ところで、Textプロパティを正しくバインドするにはどうしたらいいですか? –

答えて

0

@Clemensのおかげで、不要な命令や、Click EventHandlerプロパティを削除してコードを修正しました。 EventHandlerDependencyPropertyとして追加することはできません。したがって、すべてのデバッグがクラッシュし、すべての例外がスローされます。私はまだそれをどのように追加するかを考えている。

public string ButtonText 
{ 
    get 
    { 
     return (string)GetValue(TextProperty); 
    } 
    set 
    { 
     SetValue(TextProperty, value); 
    } 
} 

public Symbol Icon 
{ 
    get 
    { 
     return (Symbol)GetValue(SymbolIconProperty); 
    } 
    set 
    { 
     SetValue(SymbolIconProperty, value); 
    } 
} 

私もTextBlockTextSymbolIconSymbolプロパティにバインド追加しました。私は、ユーザーコントロール(MyButton)にx:Nameを与え、文字列{Binding Path=/*name of the property of the UserControl to bind*/, ElementName="MyButton"

<UserControl 
... 
x:Name="MyButton"> 
.... 
.... 
<!--only showing for the TextBlock, but I've done the same for the SymbolIcon--> 
<TextBlock Grid.Column="1" 
      Text="{Binding Path=ButtonText,ElementName=MyButton}"/> 
関連する問題