2011-04-07 8 views
3

MVVM에서 여러 개의 버튼을 동적으로 바인딩하고 싶습니다.
1.It ItemControl을 사용하여 동적으로 생성 된 버튼
2. 트리거 이벤트를 호출하지 않았습니다. 도와주세요. 당신이 XAML 어떻게 보일지에MVVM의 ItemsControl에서 EventTrigger가 작동하지 않습니다.

 <ItemsControl ItemsSource="{Binding ComponentList,Mode=TwoWay}"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <Button Tag="{Binding WorkFlowCompId}"> 
           <Button.Content> 
            <TextBlock Text="{Binding ComponentName,Mode=TwoWay}"/> 
           </Button.Content> 
           <i:Interaction.Triggers> 
            <i:EventTrigger EventName="Click"> 
             <i:InvokeCommandAction Command="{Binding ComponentSelected}" 
CommandParameter="{Binding WorkFlowCompId,Mode=TwoWay}" > 
             </i:InvokeCommandAction> 
            </i:EventTrigger> 
           </i:Interaction.Triggers> 
          </Button> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
+0

이 내가 무엇인가 Interaction.Triggers를 ? –

+0

xmlns : i = "http://schemas.microsoft.com/expression/2010/interactivity"Microsoft.Expression.Interactions for intractions을 사용하고 있습니다. – Ramakrishnan

+0

그러면 질문에 지정할 필요가있을 것입니다. –

답변

0

첫 번째 패스 : -

 <ItemsControl ItemsSource="{Binding ComponentList}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Command="{Binding SelectComponent}"> 
         <TextBlock Text="{Binding ComponentName}"/> 
        </Button> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

데릭은 자신의 의견에 당신이 컨테이너에 ComponentSelected 명령을을 암시로 나는 생각한다. 그러나 구성 요소의보기 모델에서이 명령을 이동해야합니다. 참고 나는 속성이 아닌 행동처럼 들릴 수 있도록 SelectComponent으로 이름을 변경했습니다.

TwoWay 바인딩이 제거되었으므로이 경우 아무 것도 수행하지 않습니다. 간단한 바인딩에서 태그 값을 할당하면 디자인에 문제가 있다는 경고음이 울리게됩니다.

현재 선택의 형태를 취하고 있기 때문에이 경우 ListBox이 더 적절하지 않습니까?

6

문제는 명령이 해당 템플릿에서 컨텍스트를 가져오고 거기서 ViewModel의 루트에 액세스 할 수 없다는 것입니다. 솔루션에이 클래스를 추가

public class DataContextProxy : FrameworkElement 
    { 
     public DataContextProxy() 
     { 
      this.Loaded += new RoutedEventHandler(DataContextProxyLoaded); 
     } 

     void DataContextProxyLoaded(object sender, RoutedEventArgs e) 
     { 
      Binding binding = new Binding(); 
      if (!String.IsNullOrEmpty(BindingPropertyName)) 
      { 
       binding.Path = new PropertyPath(BindingPropertyName); 
      } 
      binding.Source = this.DataContext; 
      binding.Mode = BindingMode; 
      this.SetBinding(DataContextProxy.DataSourceProperty, binding); 
     } 

     public Object DataSource 
     { 
      get { return (Object)GetValue(DataSourceProperty); } 
      set { SetValue(DataSourceProperty, value); } 
     } 

     public static readonly DependencyProperty DataSourceProperty = 
      DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null); 


     public string BindingPropertyName { get; set; } 

     public BindingMode BindingMode { get; set; } 

    } 

을 다음과 같이 XAML에서 사용 : 바인딩 명령에 다음

<UserControl.Resources> 
      <library:DataContextProxy x:Key="DataContextProxy"/> 
    </UserControl.Resources> 

:

<Button Tag="{Binding WorkFlowCompId}"> 
           <Button.Content> 
            <TextBlock Text="{Binding ComponentName,Mode=TwoWay}"/> 
           </Button.Content> 
           <i:Interaction.Triggers> 
            <i:EventTrigger EventName="Click"> 
             <i:InvokeCommandAction Command="{Binding DataSource.ComponentSelected, Source={StaticResource DataContextProxy}" 
CommandParameter="{Binding WorkFlowCompId,Mode=TwoWay}" > 
             </i:InvokeCommandAction> 
            </i:EventTrigger> 
           </i:Interaction.Triggers> 
          </Button> 
관련 문제