2011-04-09 4 views
0

<MultiBinding>을 사용하여 ViewModel의 명령에 두 개의 매개 변수를 전달하려고했지만 XAML 파서가 내 시도를 받아들이는 데 문제가 있습니다.MultiBinding 명령 매개 변수에 바인딩 바인딩 포함

UserControl 내에 포함 된 다음 ListView가 Ticket 개체의 컬렉션에 바인딩되어 있다고 가정합니다.

<ListView x:Name="lvTicketSummaries" 
        ItemsSource="{Binding TicketSummaries}" 
        ItemContainerStyle="{DynamicResource ResourceKey=ListViewItem}" 
        IsSynchronizedWithCurrentItem="True"> 

각각의 스타일 ListViewItem

<Style x:Key="ListViewItem" TargetType="{x:Type ListViewItem}"> 
    <Setter Property="ContextMenu" Value="{DynamicResource ResourceKey=cmListViewItem}"/> 
    <!-- A load of irrelevant stuff -> 
</Style> 

그리고 내 쿼리의 소스가 앉아 참조 ContextMenu 항목;

<!-- ContextMenu DataContext bound to UserControls view model so it can access 'Agents' ObservableCollection -->  
<ContextMenu x:Key="cmListViewItem" DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext}"> 
    <MenuItem Header="Send as Notification" ItemsSource="{Binding Path=Agents}"> 
     <MenuItem.ItemContainerStyle> 
      <Style TargetType="{x:Type MenuItem}"> 
       <!-- Display the name of the agent (this works) --> 
       <Setter Property="Header" Value="{Binding Path=Name}"/> 
       <!-- Set the command to that of one on usercontrols viewmodel (this works) --> 
       <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.SendTicketNotification}" /> 
       <Setter Property="CommandParameter"> 
        <Setter.Value> 
         <MultiBinding Converter="{StaticResource ResourceKey=TicketNotificationParameterConverter}"> 
          <MultiBinding.Bindings> 
           <!-- This SHOULD be the Agent object I clicked on to trigger the Command. This does NOT work, results in either exception or 'UnsetValue' --> 
           <Binding Source="{Binding}" /> 
           <!-- Also pass in the Ticket item that was clicked on to open the context menu, this works fine --> 
           <Binding RelativeSource="{RelativeSource AncestorType={x:Type ListView}}" Path="SelectedItem" /> 
          </MultiBinding.Bindings> 
         </MultiBinding> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </MenuItem.ItemContainerStyle> 
    </MenuItem> 
</ContextMenu> 

그래서 여기에 내가하려고하는 것이 있습니다.

  • 컨텍스트 메뉴를 선택하면, 모든받을 수있는 사용 가능한 Agents 말했다 통지를 나열, 단일 항목 "통지로 티켓을 보내"을 가지고있다. 이 작동합니다.

  • 나는이 에이전트 옵션 중 하나를 클릭

    , 나는 상황에 맞는 메뉴 항목이 컨텍스트 메뉴 (이 작품) 및 I로부터 선택된 Agent 항목을 보여 모두 listview에서 클릭 한 ticket 항목을 보내려면 컨텍스트 메뉴 상황에 맞는 메뉴의 실제 셋업이 나에게 복잡한 것을 몇 가지를 보이더라도 나는 지금 MultiBinding

    <MultiBinding Converter="{StaticResource ResourceKey=TicketNotificationParameterConverter}"> 
             <MultiBinding.Bindings> 
              <!-- This works, it sends the Ticket object to the Converter --> 
              <Binding RelativeSource="{RelativeSource AncestorType={x:Type ListView}}" Path="SelectedItem" /> 
              <!-- This caused an exception saying that I can only set 'Path' property on DependencyProperty types --> 
              <Binding Path="{Binding}" /> 
             </MultiBinding.Bindings> 
            </MultiBinding> 
    

으로이 절반 달성했다. MultiBinding 매개 변수 중 하나가 실제 클릭 한 항목 인 ContextMenu.MenuItem에 바인딩되어야한다는 실제 사양은 아주 간단한 명령처럼 보입니다. 컨텍스트 메뉴에는 모든 에이전트가 올바르게 나열되어 있으므로이 현재 개체를 명령 매개 변수로 보내도록 요청하면 간단하다고 생각합니다. 나는 또한 시도했다; 매개 변수 계산기로 전송됩니다

<Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}} Path="SelectedItem" /> 

뿐만 아니라

<Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}} Path="SelectedItem" /> 

그러나 모든

UnsetValue 당신의 시간 및 당신이 가진 수있는 제안을 주셔서 감사합니다.

답변

3

이 줄은 홀수 :

<Binding Path="{Binding}" /> 

현재의 DataContext 사실 하지 문자열 결국에있는 현재의 DataContext에 뭔가를 찾을 것 경로로 충분 문자열인가? 완벽하게 이해합니다.

DataContext에 바인딩하는 대신이 작업을 수행 했습니까?

<Binding /> 
관련 문제