2009-12-29 45 views
0

我最近重構了一個DataTrigger。它以前的DataContext設置爲ListBoxItem。現在它是一個ContentPresenterWPF - 爲觸發器的setter設置DataContext

下面是代碼:

<DataTemplate.Triggers> 
    <DataTrigger Value="True"> 
     <DataTrigger.Binding> 
      <MultiBinding Converter="{StaticResource DisableWorkItemConverter}"> 
       <Binding ElementName="MainForm" Path="PickedWorkItemID"/> 
       <Binding Path="WorkItemForColumn.Id"/> 
      </MultiBinding> 
     </DataTrigger.Binding> 
     <Setter Property="IsEnabled" Value="False"/> 
     <Setter Property="loc:Main.IsCurrentItemEnabledChanged" Value="True"/> 
    </DataTrigger> 
</DataTemplate.Triggers> 

IsCurrentItemEnabledChangedOnChange我可以看到,我可以在ListBoxItem用下面的代碼獲得(在後面的代碼):

listBoxItem = (ListBoxItem)Main.Instance.lstQueryResults.ItemContainerGenerator. 
       ContainerFromItem(((ContentPresenter)d).Content); 

然而,我無法看到將DataContext設置爲使我的SetterIsEnabled將設置ListBoxItem的啓用狀態而不是ContentPresenter

(我會做它的IsCurrentItemEnabledChangedOnChange,但該財產已經是一個黑客攻擊的一位,當觸發設置爲false也不會重新啓用的項目。)

任何想法?


有人建議我提供更多的提供方面:

這裏是我的模板XAML。

<DataTemplate x:Key="ColumnTemplate"> 
    <Border Name="ItemBorder" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Padding="2"> 
     <WrapPanel>     
      <TextBlock Margin="0,0,5,0"> 
       <TextBlock.Text> 
        <Binding Path="Name" Converter="{StaticResource GetVisibilityOfColumnTitles}"/> 
       </TextBlock.Text> 
      </TextBlock> 
      <TextBlock Text="{Binding Value}" Margin="0,0,10,0" FontWeight="Bold" /> 
     </WrapPanel> 
    </Border> 
</DataTemplate> 

<DataTemplate x:Key="RowTemplate"> 
    <Border x:Name="ItemBorder" BorderThickness="1" BorderBrush="#D4D4FF"> 
     <Grid x:Name="ItemGrid" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsPresenter}}, Path=ActualWidth}" ScrollViewer.CanContentScroll="True" Margin="2"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="20" /> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="30" /> 
      </Grid.ColumnDefinitions> 
      <Grid.Background> 
       <Binding Path="WorkItemForColumn.Type" Converter="{StaticResource WorkItemTypeToColorConverter}" /> 
      </Grid.Background> 

      <CheckBox VerticalAlignment="Center" Grid.Column="0" IsChecked="{Binding 
             RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, 
             Mode=TwoWay, Path=IsSelected}" Name="chkIsSelected" /> 

      <ItemsControl Grid.Column="1" Margin="5,0,5,0" ItemsSource="{Binding}" ItemTemplate="{StaticResource ColumnTemplate}" > 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <WrapPanel Orientation="Horizontal"></WrapPanel> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 


      <Button HorizontalAlignment="Right" x:Name="btnPick" Grid.Column="3" Style="{StaticResource roundButton}" Width="15" Height="15" Tag="{Binding WorkItemForColumn.Id}" Margin="5,0,10,0"> 
       <Path Fill="DarkBlue"> 
        <Path.Data> 
         <PathGeometry> 
          <PathFigure StartPoint="2,0" IsClosed="True"> 
           <LineSegment Point="7,5"/> 
           <LineSegment Point="2,10"/> 
          </PathFigure> 
          <PathFigure StartPoint="2,2" IsClosed="True"> 
           <LineSegment Point="5,5"/> 
           <LineSegment Point="2,8"/> 
          </PathFigure> 
         </PathGeometry> 
        </Path.Data> 
       </Path> 
      </Button> 
     </Grid> 
    </Border> 
    <DataTemplate.Triggers> 
     <DataTrigger Value="True"> 
      <DataTrigger.Binding> 
       <MultiBinding Converter="{StaticResource DisableWorkItemConverter}"> 
        <Binding ElementName="MainForm" Path="PickedWorkItemID"/> 
        <Binding Path="WorkItemForColumn.Id"/> 
       </MultiBinding> 
      </DataTrigger.Binding> 
      <Setter Property="IsEnabled" Value="False"/> 
      <Setter Property="loc:Main.IsCurrentItemEnabledChanged" Value="True"/> 
     </DataTrigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 

這裏是列表框中的XAML:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" Button.Click="PickWorkItem_Click" SelectionMode="Multiple" ItemTemplate="{StaticResource RowTemplate}" Name="lstQueryResults" SelectionChanged="lstQueryResults_SelectionChanged" > 
    <ListBox.Resources> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/> 
      </Style.Resources> 
     </Style> 
    </ListBox.Resources> 
</ListBox> 

DataContext的是在代碼中設置像這樣:

private void ChangeQueryResultListSource(WorkItemCollection queryResults, bool b) 
{ 
    // Un-hook the selection event while we change the ItemsSource 
    Form.ToggleOnSelectEvent(false); 

    // This sets the DataContext 
    QueryDisplay = GetDisplayValues(queryResults); 

    Form.QueryResultListSource = QueryDisplay; 
    // Rewire the selection events back in 
    Form.ToggleOnSelectEvent(true); 


    foreach (WorkItem item in Pad.Keys) 
    { 
     Form.SelectQueryResultItem(item); 
    } 
} 

private List<List<WorkItemColumn>> GetDisplayValues(WorkItemCollection queryResults) 
{ 
    var result = new List<List<WorkItemColumn>>(); 
    foreach (WorkItem workItem in queryResults) 
    { 
     var row = GetQueryColumns(queryResults.DisplayFields, workItem); 
     result.Add(row); 
    } 
    return result; 
} 

private List<WorkItemColumn> GetQueryColumns(DisplayFieldList fields, WorkItem workItem) 
{ 
    var row = new List<WorkItemColumn>(); 
    foreach (FieldDefinition column in fields) 
    { 
     var workItemColumn = new WorkItemColumn { Name = column.Name, Value = workItem[column.Name], WorkItemForColumn = workItem }; 
     row.Add(workItemColumn); 
    } 

    return row; 
} 

WorkItemColumn是一個名稱 - 值對一類和對數據的引用(一個WorkItem對象)。

+0

縮小了一點,顯示出周圍的代碼,顯示整個'DataTemplate'和顯示正在使用它的控件。 – 2009-12-29 20:29:26

+0

添加了所需的代碼。感謝您看看這個。 – Vaccano 2009-12-29 21:04:09

+0

我可能是唯一一個,但我有點困惑你的問題。 「但是,我無法設置DataContext,因此我的Setter for IsEnabled將設置ListBoxItem而不是ContentPresenter的啓用狀態。」 – Ragepotato 2009-12-29 21:11:02

回答

2

對不起,我簡單的回答,但你應該移動觸發和setter邏輯IsEnabledListBoxItem風格:

<Style TargetType="{x:Type ListBoxItem}"> 
    <Style.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/> 
    </Style.Resources> 
    <Style.Triggers> 
     <DataTrigger Value="True"> 
      <DataTrigger.Binding> 
       <MultiBinding Converter="{StaticResource DisableWorkItemConverter}"> 
        <Binding ElementName="MainForm" Path="PickedWorkItemID"/> 
        <Binding Path="WorkItemForColumn.Id"/> 
       </MultiBinding> 
      </DataTrigger.Binding> 
      <Setter Property="IsEnabled" Value="False"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
+0

謝謝!那樣做了! – Vaccano 2009-12-29 22:37:00