2011-05-01 93 views
0

在互聯網上,我發現了很多樣式化列表視圖中完整列或完整行的示例。在列表視圖中設置單個單元格

我需要能夠在列表視圖中動態設置單個單元格。我如何訪問連續單個項目的屬性?

+1

這將是困難的/有問題的,原因很簡單,顧名思義,ListView控件是所有關於觀看列表,嗯,一個特定的細胞,被視爲「只是一個集合中的一個數據點」。如果您試圖將ListView擴展到電子表格,那麼您正在設置自己的失敗。如果你想要的是一個電子表格,那麼我建議你搜索一個Windows Forms電子表格控件。只是我的愚見。 – corlettk 2011-05-01 12:33:43

+1

@corlettk,爲什麼要在WPF應用程序中使用WinForms控件?如果沒有其他辦法,我認爲這只是最後的手段。 – svick 2011-05-01 14:18:26

回答

0

一般來說,你不應該需要這個。假設您使用的是GridView,那麼您應該可以使用GridViewColumnCellTemplateCellTemplateSelector。如果你真的想訪問特定的單元格,我認爲沒有乾淨的方法,你最好使用DataGrid(來自.Net 4或者.Net 3.5的WPF工具包)。有了這一點,你可以做這樣的事情:

((TextBlock)datagrid.Columns[1].GetCellContent(m_specificItem)).Background = Brushes.Red 
+0

我原本以爲是用DataGrid來做,但我讀到ListView和DataGrid之間的區別就在於編輯。我不需要任何編輯功能。我只需要以很好的方式呈現數據。我猜DataGrid可能會導致很多開銷。對? Jeff – Jeff 2011-05-01 16:32:55

+0

我試過一個DataGrid,但是下面的代碼給出了一個錯誤:((TextBlock)dgFinancialData.Columns [1] .GetCellContent(「ID」))。Background = Brushes.YellowGreen;錯誤:對象引用未設置爲對象的實例。我有3列7行。 – Jeff 2011-05-01 18:41:26

+0

@Jeff,GetCellContent()的參數必須是網格的Items集合中的一個項目(通過設置ItemsSource屬性也可以將其分配給它)。 – svick 2011-05-01 19:51:34

2

如果您希望使用數據對象中的數量有限的屬性來設置項目的樣式,則可以創建數據模板和樣式,並使用數據觸發器在它們之間進行切換。我已經使用類似這樣的方法來根據列表中的數據對象是否處於「活動/不活動」狀態來改變列表中數據對象的外觀,並根據它是否被選中來創建對象的摺疊/展開版本。

您還可以使用轉換器(內置或自定義)輕鬆獲得某些效果。例如,我使用內置的布爾到可見性轉換器來根據對象的IsActive成員來隱藏/取消隱藏我的TaskSelectedTemplate中的組合框/文本塊。

<DataTemplate x:Key="TaskSelectedTemplate"> 
    <Grid Margin="4"> 
     ... 
     <Border Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Margin="0 0 4 0" 
       BorderThickness="0" 
       CornerRadius="2"> 
      <Border.Background> 
       <MultiBinding Converter="{StaticResource ActiveToColor}"> 
        <Binding Path="."/> 
        <Binding Path="IsActive"/> 
        <Binding Path="IsPaused"/> 
       </MultiBinding> 
      </Border.Background> 
     </Border> 

     <StackPanel Grid.Row="0" Grid.Column="1" 
        Orientation="Horizontal" 
        Margin="0 2"> 
      <ComboBox ItemsSource="{Binding Source={StaticResource TaskTypes}}" 
         SelectedItem="{Binding Type}" 
         Text="{Binding Type}" 
         Visibility="{Binding IsActive, Converter={StaticResource BoolToVis}}"/> 
      <TextBlock Text="{Binding Type}" 
         FontWeight="Bold" 
         Visibility="{Binding IsActive, Converter={StaticResource InvBoolToVis}}"/> 
      <TextBlock Text=" task"/> 
     </StackPanel> 
     ... 
    </Grid> 
</DataTemplate> 

<DataTemplate x:Key="TaskNotSelectedTemplate"> 
    <Grid Margin="4"> 
     ... 
     <Border Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Margin="0 0 4 0" 
       BorderThickness="0" 
       CornerRadius="2"> 
      <Border.Background> 
       <MultiBinding Converter="{StaticResource ActiveToColor}"> 
        <Binding Path="."/> 
        <Binding Path="IsActive"/> 
        <Binding Path="IsPaused"/> 
       </MultiBinding> 
      </Border.Background> 
     </Border> 

     <TextBlock Grid.Row="0" Grid.Column="1" 
        Text="{Binding Type}"/> 
     <TextBlock Grid.Row="0" Grid.Column="2" 
        TextAlignment="Right"> 
       <Run Text="{Binding Length.TotalMinutes, StringFormat='0', Mode=OneWay}"/> 
       <Run Text=" min"/> 
      </TextBlock> 
     <TextBlock Grid.Row="0" Grid.Column="3" 
        TextAlignment="Right"> 
       <Run Text="{Binding TimesPerformed, Mode=OneWay}"/> 
       <Run Text=" tasks"/> 
      </TextBlock>    
    </Grid> 
</DataTemplate> 

<Style x:Key="ContainerStyle" TargetType="{x:Type ListBoxItem}"> 
    <!--this part changes the selected item highlight color--> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border Name="Border"> 
        <ContentPresenter /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter TargetName="Border" 
           Property="Background" Value="#2000BFFF"> 
         </Setter> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <!--this part causes selected task to expand--> 
    <Setter Property="ContentTemplate" Value="{StaticResource TaskNotSelectedTemplate}"/> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="ContentTemplate" Value="{StaticResource TaskSelectedTemplate}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

對於更復雜的情況,您可能要查看DataTemplateSelector。我從來沒有使用它,但它似乎是理想的,如果你有很多數據模板來玩弄。

相關問題