2010-07-13 4 views
2

XAML에서 메서드 처리기를 할당 할 수 있도록 Func 유형의 종속성 속성을 추가하려는 사용자 정의 컨트롤이 있습니다. 그러나이 경우 XAMLParseException이 발생합니다. 'Func`2'형식에는 공용 TypeConverter 클래스가 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? Func 용 TypeConverter를 구현해야합니까, 아니면 더 좋은 방법이 있습니까?사용자 정의 컨트롤의 Func 속성

사용자 컨트롤에 Func을 종속성 속성 (을 MyUserControl)

<Window x:Class="FuncTest.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:FuncTest="clr-namespace:FuncTest" 
    Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <FuncTest:MyUserControl MyFunc="SquareHandler" /> 
    </Grid> 
</Window> 

코드 숨김 :

namespace FuncTest 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      SquareHandler = (arg => arg * arg); 

      DataContext = this; 
     } 

     public Func<int, int> SquareHandler { get; set; } 
    } 
} 

답변

5
MyFunc="SquareHandler" 
민주당, XAML을 사용

public Func<int, int> MyFunc 
{ 
    get { return (Func<int, int>)GetValue(MyFuncProperty); } 
    set { SetValue(MyFuncProperty, value); } 
} 

public static readonly DependencyProperty MyFuncProperty = 
    DependencyProperty.Register("MyFunc", 
           typeof(Func<int, int>), 
           typeof(SillyCtrl), 
           new UIPropertyMetadata(null)); 

"MyFunc"속성을 "SquareH"로 설정합니다. andler "문자열하고 Funcs로 문자열을 변환 할 수있는 TypeConverter가 당신을 묻는 그 이유는이, 현재의 DataContext의 SquareHandler 속성을 사용하는

<FuncTest:MyUserControl MyFunc="{Binding SquareHandler}" /> 

로 변경.

관련 문제