2012-07-26 3 views
1

DataTemplateSelector를 사용하여 항목에 대해 렌더링 할 데이터를 결정할 때 사용할 listBox에 대한 두 개의 데이터 템플릿을 정의했습니다. 그러나 내 템플릿 중 하나에는 포함 된 버튼이있어이를 클릭하면 양식이 제자리에 표시됩니다. 그래서 부울 속성을 설정하고 INotifyPropertyChanged를 구현했습니다. boolean 속성이 true 일 때 현재 템플릿은 세 번째 템플릿으로 대체됩니다.datatemplate이 데이터 트리거를 사용하지 않음

이것은 버튼의 코드입니다.

 Activity activityItem = (Activity)listBox1.Items[listBox1.SelectedIndex]; 
     activityItem.ShowFeedbackForm = 1; 

내 XAML이다 : 사용자가 버튼을 클릭 할 때, (1)의 값이 피드백 형태를 나타내는 템플릿이되는 경우이다 해당 항목 ShowFeedbackForm 설정되어

<Grid.Resources> 
    <DataTemplate x:Key="completedActivityTemplate"> 
     <Grid Name="templateGrid" > 
    ... 
    <DataTemplate x:Key="activityTemplate"> 
     <Grid Name="templateGrid" > 
    ... 
    <DataTemplate x:Key="feedbackFormTemplate"> 
     <Grid Name="templateGrid" > 
    ...  
    <Style TargetType="ListBoxItem" x:Key="ContainerStyle"> 
     <Setter Property="Height" Value="55" /> 
     <Setter Property="Background" Value="Transparent" /> 
     <Style.Triggers> 
      <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
       <Setter Property="IsSelected" Value="True" /> 
      </Trigger> 
      <DataTrigger Binding="{Binding showFeedbackForm}" Value="1"> 
       <Setter Property="ContentTemplate" Value="{StaticResource feedbackFormTemplate}" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
    </Grid.Resources> 
    <ListBox Grid.Row="1" Name="listBox1" IsSynchronizedWithCurrentItem="True" 
     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     VerticalContentAlignment="Stretch" Background="Transparent" 
     BorderThickness="0" ItemContainerStyle="{StaticResource ContainerStyle}" 
     HorizontalContentAlignment="Stretch" Margin="-3,0,0,0" 
     ItemTemplateSelector="{StaticResource myDataTemplateSelector}"> 
    </ListBox> 

표시되지만 아무것도 일어나지 않습니다. 내 ObservableCollection (편도) 바인딩이 제대로 작동합니다.

답변

0

버튼 클릭 핸들러에서 부울 값을 바인딩 된 항목으로 설정 한 다음 listBox에 대한 데이터 템플릿 선택기를 리 바인드합니다.

 ListBoxItem currentItem = (ListBoxItem)(listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items.CurrentItem)); 
     Activity activityItem = (Activity)listBox1.Items[listBox1.SelectedIndex]; 
     activityItem.ShowFeedbackForm = 1; 
     listBox1.ItemTemplateSelector = new ActivityFeedDataTemplateSelector(); 

부울을 설정하면 내 ActivityFeedDataTemplateSelector에서 원하는 템플릿 이름을 반환합니다.

관련 문제