views:

667

answers:

1

I have a list box that displays the results of a TFS Query. I want to change the style of the ListBoxItem in the code behind to have the columns that are included in the query results.

The style for the ListBoxItem is defined in my Windows.Resoruces Section. I have tried this:

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 ----------------------------------------------+

But the ContentTemplate is null. I got that code from here, then modified it with the LoadContent call (the orginal code gave null for the ContentPresenter).

Anyway. If you know a way to change an existing style in the code behind I would love to see it.


Specifics if you want them:
I am going for WrapPanel in my ListBoxItem Style. This is what I want to add the extra TextBlock items to.

Here is part of my style:

<!--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>
+4  A: 

You're going against the grain here, trying to manipulate visual elements directly in code-behind. There's a much simple solution involving data binding.

I'll provide the general solution because I don't know the specifics of your solution.

Once you get your query results, create an enumeration that returns a column name, and a field value for each iteration.

Example:

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] };
}

And in your code-behind, once you get your DataTable result set, do this:

myResultsList.ItemsSource = EnumerateResultSet(myDataTable);

The XAML might look like this:

<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:

Sample Output Image

Aviad P.
This worked great! Whats more, this is a fantastic answer. I have set it as the answer, but I cannot upvote it due to an issue with Stack Overflow. I have submitted it as a bug on meta: http://meta.stackoverflow.com/questions/34009/cant-upvote-an-answer-says-the-vote-it-too-old Thank you so much for taking the time to help me out with this. WPF is great when you are doing it the correct way, but it is bear when you are on the wrong path. Thanks for showing me the path...
Vaccano
You're very right about your last statement :)And you're welcome!
Aviad P.