2009-12-28 123 views
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> 

回答

11

你要對糧食這裏,試圖直接在代碼隱藏操作的視覺元素。有一個涉及數據綁定的簡單解決方案。

我將提供一般解決方案,因爲我不知道解決方案的具體情況。

一旦得到查詢結果,就創建一個枚舉,該枚舉返回列名和每次迭代的字段值。

例子:

class NameValuePair 
{ 
    public string Name { get; set; } 
    public object Value { get; set; } 
} 

public IEnumerable<IEnumerable<NameValuePair>> EnumerateResultSet(DataTable resultSet) 
{ 
    foreach (DataRow row in resultSet.Rows) 
     yield return EnumerateColumns(resultSet, row); 
} 

public IEnumerable<NameValuePair> EnumerateColumns(DataTable resultSet, DataRow row) 
{ 
    foreach (DataColumn column in resultSet.Columns) 
     yield return new NameValuePair 
      { Name = column.ColumnName, Value = row[column] }; 
} 

而在你的後臺代碼,一旦你得到你的數據表的結果集,這樣做:

myResultsList.ItemsSource = EnumerateResultSet(myDataTable); 

的XAML可能是這樣的:

<Window.Resources> 
    <DataTemplate x:Key="ColumnTemplate"> 
     <Border BorderBrush="Black" BorderThickness="1" CornerRadius="2" Padding="2"> 
      <WrapPanel> 
       <TextBlock Text="{Binding Name}" Margin="0,0,5,0"/> 
       <TextBlock Text="{Binding Value}" Margin="0,0,10,0"/> 
      </WrapPanel> 
     </Border> 
    </DataTemplate> 
    <DataTemplate x:Key="RowTemplate"> 
     <Grid> 
      <ItemsControl 
       ItemsSource="{Binding}" 
       ItemTemplate="{StaticResource ColumnTemplate}" 
       Margin="0,5,0,5"/> 
     </Grid> 
    </DataTemplate> 
</Window.Resources> 
<Grid> 
    <ListBox Name="myResultsList" ItemTemplate="{StaticResource RowTemplate}"/> 
</Grid> 

樣品輸出:

Sample Output Image

+1

這很好用!更何況,這是一個夢幻般的答案。我已經將它設置爲答案,但由於堆棧溢出問題,我無法對它作出迴應。我已經提交它作爲一個錯誤元: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

+0

你對你最後的聲明是非常正確的:) 不客氣! – 2009-12-28 21:57:10