2017-04-25 13 views
2

두 개의 버튼이있는 사용자 정의 창이 있습니다. 한 버튼의 이름은 OKButton이고 다른 버튼의 이름은 취소 버튼입니다.사용자 정의 컨트롤의 요소를보기에 액세스 가능하게 설정하여 명령에 구독하는 방법?

<Style TargetType="{x:Type WindowCustom}"> 
    "Properties Here" 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type WindowCustom}"> 
       <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
            <Button x:Name="OKButton" Content="OK"/> 
            <Button x:Name="CancelButton" Content="Cancel"/> 
"Closing Tags" 

내가 CLR 속성이있는 템플릿 부분을 만든 그 OnApplyTemplate 메서드에서 인스턴스를 가져옵니다 "OKButton"에 대한.

private const string OKButtonPart = "PART_OKButton"; 

    private Button oKButton; 

    public Button OKButton 
    { 
     get { return oKButton; } 
     set 
     { 
      if (oKButton != null) 
      { 
       oKButton.Click += OKButtonClick; 
       oKButton.Loaded += OKButtonLoaded; 
      } 

      oKButton = value; 
     } 
    } 
    public override void OnApplyTemplate() 
    { 
     OKButton = GetTemplateChild(OKButtonPart) as Button; 
    } 

사용자 지정 창을 만드는 데 필요한 다른 모든 코드가 있다고 가정합니다. 내 OKButton을 내가 원하는대로 할 수 있도록 몇 가지 라우트 된 명령을 작성했습니다. 버튼의 내 이전 구현

<Button> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <cal:ActionMessage MethodName="SaveHistoryEntry" /> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button> 

가 어떻게 동작 메시지에 추가 XAML을 통해 내 컨트롤에 액세스 할 ActionMessage (명령을 말하는 Caliburns 방식)를 사용하기 때문에 적합하지 않습니다? 내가 할 수있는 것은 사용자 정의 윈도우에 내 버튼 컨트롤의 이름을 쓰는 것입니다.

<lc:WindowCustom OKButton=""> 

여기에서 무엇을해야할지 모르겠다.

답변

2

당신은 당신의 WindowCustom 클래스에 종속성 속성을 추가 할 수있는 ButtonCommand 속성을

public static readonly DependencyProperty OkCommandProperty = 
    DependencyProperty.Register("OkCommand", typeof(ICommand), 
    typeof(CustomWindow), new FrameworkPropertyMetadata(null)); 

public ICommand OkCommand 
{ 
    get { return (ICommand)GetValue(OkCommandProperty); } 
    set { SetValue(OkCommandProperty, value); } 
} 

... 그리고 바인딩 ControlTemplate 일이에 :

<Button x:Name="OKButton" Content="OK" Command="{Binding OkCommand, RelativeSource={RelativeSource TemplatedParent}}"/> 

그런 다음 수 윈도우의 의존성 프로퍼티를 임의의 ICommand 소스 프로퍼티로 설정하거나 바인딩한다 :

<lc:WindowCustom OkCommand="{Binding YourViewModelCommandProperty}"> 

Button을 클릭하면 명령이 호출됩니다. 물론 Button 취소와 동일한 작업을 수행 할 수 있습니다. 다른 종속성 속성을 추가하기 만하면됩니다.

+1

곧 캘리브레이션으로 구현을 게시 할 예정입니다. 고마워 친구! – axelrotter

2

이 대답은 사용자 지정 컨트롤에 ActionMessage 기능을 사용하려는 Caliburn 사용자을 대상으로합니다. CustomWindow에이

<lc:ButtonCustom x:Name="PART_OKButton"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <cal:ActionMessage MethodName="{Binding OkCommand, RelativeSource={RelativeSource TemplatedParent}}" /> 
       </i:EventTrigger> 
       </i:Interaction.Triggers> 
</lc:ButtonCustom> 

C# 코드가 MM8의 도움말과 거의 동일처럼 내 사용자 지정 창에 놓여 버튼이 보인다.

public string OkCommand 
    { 
     get { return (string)GetValue(OkCommandProperty); } 
     set { SetValue(OkCommandProperty, value); } 
    } 



public static readonly DependencyProperty OkCommandProperty = DependencyProperty.Register("OkCommand", typeof(string), typeof(WindowCustom), 
      new FrameworkPropertyMetadata(null)); 

ActionMessage가 문자열을 허용하기 때문에 ICommand를 String 데이터 유형으로 변경했습니다.

마지막으로 창에서 동작 메시지에 원하는 동작을 할당합니다.

<lc:WindowCustom <!--xmlns tags and other dependency proerties--> 
    OkCommand="SaveHistoryEntry"> 

It Works!