2016-08-14 2 views
3
에서 C#을 바인딩 양식

나는 다음과 같은 XAML 내보기 모델의 삭제 명령에 결합하기 위해 노력하고있다 : 나는 그래서 프로그래밍 사용할 수있는 C#으로 변환하기 위해 노력하고있어자 마린은 XAML ContextAction가 ViewCell

 <TextCell.ContextActions> 
      <MenuItem Command="{Binding Path=BindingContext.DeleteCollectionCommand, Source={x:Reference Name=CollectionListView}}" 
        CommandParameter="{Binding .}" 
        Text="Delete" 
        IsDestructive="True" /> 
     </TextCell.ContextActions> 

그것.

다음을 시도했지만 작동하지 않습니다. 무엇을 변경해야합니까? ViewModel DeleteCommand에 액세스하는 더 나은/다른 방법이 있습니까?

protected override void OnBindingContextChanged() 
    { 
     base.OnBindingContextChanged(); 

     var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background 
     deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding(".")); 
     deleteAction.SetBinding(MenuItem.CommandProperty, 
      new Binding("BindingContext.DeleteCommand", BindingMode.Default, null, null, null, "{x:Reference Name=CollectionBeerListView}")); 

     ContextActions.Add(deleteAction); 
    } 

편집 내가 부모보기에서이 같은 셀을 SKAR의 답변을 결합하고 초기화하여이 작업을 얻을 수 있었다

:

 lstView.ItemTemplate = new DataTemplate(() => 
     { 
      var cell = new DeleteGenericListItemTemplate(page); 
      return cell; 
     }); 

이 이상적입니다 확실하지. 하지만 계속 움직여.

+1

그럼 내가 할 바인딩 스로우 만드는 법을 모른다. Xaml을 사용하는 C#은 훨씬 쉽고 직선적인데, 내가 본 이상한 바인딩이라고해야한다. 질문의 두 번째 부분을 도와 드릴 수 있습니다. viewmodel에 필요한 속성에 도달하려면 BindingContext = new (your_ViewModel)을 설정해야합니다. 그렇게하면 쉽게 바인딩 할 수 있습니다. 더 잘 도와 드릴 수 있도록 코드 숨김 및 Xaml을 더 많이 사용할 수 있습니까? Xaml 대신 C#으로 왜이 작업을 원하십니까? – BraveHeart

+0

내가 프로그래밍 방식으로 표시/숨기기 삭제 동작을 할 수 있어야합니다. C#에서이 작업을 수행하고 있습니다. 이것은 여러 장소에서 사용하고있는 일반 뷰 셀입니다. – aherrick

+0

주어진 View Cell 컨텍스트 내에서 View Model의 "Delete Command"에 액세스해야합니다. – aherrick

답변

1

당신이 당신의 페이지에 참조하여 아래 같은 것을 만들 수있는 텍스트 셀을 확장 할 경우 페이지의 바인딩 컨텍스트를 통해 뷰 모델에 DeleteCommand에 액세스 할 수 있어야합니다 :

public class CustomCell: TextCell 
{ 
     public CustomCell(Page page) 
     { 
      var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background 
      deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding(".")); 
      deleteButton.SetBinding (MenuItem.CommandProperty, new Binding ("BindingContext.DeleteCommand", source: page)); 
      ContextActions.Add(deleteAction); 


     } 
} 
+0

응답 해 주셔서 감사합니다. 실제로 "Page"객체를 전달해야합니까? 그렇다면 어떻게이 변수를 전달하는 XAML에서이 사용자 정의 셀을 호출할까요? – aherrick

+0

내가 생각하기에 코드를 사용해야 할 것입니다. 새 편집과 마찬가지로 글에 게시했습니다. – skar

관련 문제