3
我有一個顯示TFS查詢結果的列表框。我想在後面的代碼中更改ListBoxItem的樣式,以使查詢結果中包含的列。WPF - 在代碼後面更改代碼樣式
ListBoxItem的樣式在Windows.Resoruces節中定義。我試過這個:
public T GetQueryResultsElement<T>(string name) where T : DependencyObject
{
ListBoxItem myListBoxItem =
(ListBoxItem)(lstQueryResults.ItemContainerGenerator.ContainerFromIndex(0));
// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter =
myListBoxItem.Template.LoadContent().FindVisualChild<ContentPresenter>();
// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; <------+
T myControl = (T)myDataTemplate.FindName(name, myContentPresenter); |
|
return (T)myControl; |
} |
|
ContentTemplate is null ----------------------------------------------+
但是ContentTemplate爲null。我從here獲得了該代碼,然後使用LoadContent調用(原始代碼爲ContentPresenter提供了空值)對其進行了修改。
無論如何。如果你知道一種方法來改變背後代碼中的現有風格,我很樂意看到它。
具體細節,如果你希望他們:
我在我的一個ListBoxItem風格去爲WrapPanel。這是我想要添加額外的TextBlock項目。
這裏是我的風格部分:
<!--Checkbox ListBox-->
<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
<Style.Resources>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Tag" Value="{Binding Id}"/>
<Setter Property="Background">
<Setter.Value>
<Binding Path="Type" Converter="{StaticResource WorkItemTypeToColorConverter}" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border BorderThickness="1" BorderBrush="#D4D4FF">
<Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WrapPanel}}, Path=ActualWidth}" ScrollViewer.CanContentScroll="True" Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<Grid.Background>
<Binding Path="Type" Converter="{StaticResource WorkItemTypeToColorConverter}" />
</Grid.Background>
<CheckBox VerticalAlignment="Center" Grid.Column="0" IsChecked="{Binding IsSelected,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay}" Name="chkIsSelected" />
<WrapPanel Grid.Column="1" Margin="5,0,5,0" Name="QueryColumns">
<TextBlock VerticalAlignment="Center" Text="{Binding Id}" Name="txtID" />
<TextBlock VerticalAlignment="Center" Margin="5,0,5,0" Text="{Binding Title}" Name="txtTitle" />
</WrapPanel>
這很好用!更何況,這是一個夢幻般的答案。我已經將它設置爲答案,但由於堆棧溢出問題,我無法對它作出迴應。我已經提交它作爲一個錯誤元:http://meta.stackexchange.com/questions/34009/cant-upvote-an-answer-says-the-vote-it-too-old非常感謝你採取有時間幫助我解決這個問題。當你以正確的方式進行WPF時,WPF是非常棒的,但是當你走錯了路時,WPF會很好。謝謝你給我展示的路徑... – Vaccano 2009-12-28 21:55:30
你對你最後的聲明是非常正確的:) 不客氣! – 2009-12-28 21:57:10