回答
一般來說,你不應該需要這個。假設您使用的是GridView
,那麼您應該可以使用GridViewColumn
的CellTemplate
或CellTemplateSelector
。如果你真的想訪問特定的單元格,我認爲沒有乾淨的方法,你最好使用DataGrid
(來自.Net 4或者.Net 3.5的WPF工具包)。有了這一點,你可以做這樣的事情:
((TextBlock)datagrid.Columns[1].GetCellContent(m_specificItem)).Background = Brushes.Red
我原本以爲是用DataGrid來做,但我讀到ListView和DataGrid之間的區別就在於編輯。我不需要任何編輯功能。我只需要以很好的方式呈現數據。我猜DataGrid可能會導致很多開銷。對? Jeff – Jeff 2011-05-01 16:32:55
我試過一個DataGrid,但是下面的代碼給出了一個錯誤:((TextBlock)dgFinancialData.Columns [1] .GetCellContent(「ID」))。Background = Brushes.YellowGreen;錯誤:對象引用未設置爲對象的實例。我有3列7行。 – Jeff 2011-05-01 18:41:26
@Jeff,GetCellContent()的參數必須是網格的Items集合中的一個項目(通過設置ItemsSource屬性也可以將其分配給它)。 – svick 2011-05-01 19:51:34
如果您希望使用數據對象中的數量有限的屬性來設置項目的樣式,則可以創建數據模板和樣式,並使用數據觸發器在它們之間進行切換。我已經使用類似這樣的方法來根據列表中的數據對象是否處於「活動/不活動」狀態來改變列表中數據對象的外觀,並根據它是否被選中來創建對象的摺疊/展開版本。
您還可以使用轉換器(內置或自定義)輕鬆獲得某些效果。例如,我使用內置的布爾到可見性轉換器來根據對象的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。我從來沒有使用它,但它似乎是理想的,如果你有很多數據模板來玩弄。
- 1. 沒有在列表視圖中設置單元格高度
- 2. 在表視圖單元格內的UIImageView中設置圖像
- 3. 如何在表格視圖行中放置多個單元格?
- 4. 設置特定表格單元格的圖像視圖
- 5. 設置表格視圖單元格圖像
- 6. 在表格視圖單元格中嵌入表格視圖
- 7. 如何設置表格視圖單元佔據整個屏幕?
- 8. 在表格視圖單元格中使用按鈕出列單元格
- 9. 如何在Android網格視圖中設置單元格大小?
- 10. 在單個列表視圖中設置多個textview android
- 11. 爲每個單一列表視圖元素設置onClickListener
- 12. 如何設置表格視圖單元格以適應標籤?
- 13. 如何設置用於表格視圖的單元格
- 14. 設置標識符時表格視圖單元格錯誤
- 15. 如何在表格視圖單元格背景中設置圖像動畫
- 16. 複製列表視圖單元格
- 17. 迭代列表視圖單元格值
- 18. 在桌面視圖單元格中設置圖像大小
- 19. 如何在pygtk樹視圖中設置單個單元格的顏色?
- 20. 如何在Qt項目視圖中爲單個單元格設置委託?
- 21. 在表格視圖單元格中查找UIView的x位置
- 22. 將多個圖像從SQL設置爲表格視圖單元格
- 23. 設置高度後設置單元格子視圖位置?
- 24. 單個集合視圖單元格內的控制表視圖
- 25. 在自定義表格單元格中設置uiButton圖像
- 26. 將數據保存在表格視圖中單元格視圖
- 27. 如何在表格視圖單元格中設置色調/高光顏色?
- 28. 如何在iOS中設置表格視圖單元格屬性的初始值
- 29. 設置表格單元格中單選按鈕的位置
- 30. 如何在表格視圖單元格上設置突出顯示的圖像?
這將是困難的/有問題的,原因很簡單,顧名思義,ListView控件是所有關於觀看列表,嗯,一個特定的細胞,被視爲「只是一個集合中的一個數據點」。如果您試圖將ListView擴展到電子表格,那麼您正在設置自己的失敗。如果你想要的是一個電子表格,那麼我建議你搜索一個Windows Forms電子表格控件。只是我的愚見。 – corlettk 2011-05-01 12:33:43
@corlettk,爲什麼要在WPF應用程序中使用WinForms控件?如果沒有其他辦法,我認爲這只是最後的手段。 – svick 2011-05-01 14:18:26