ListBox Manipulation

G

Gordon Padwick

I'm creating a WPF application that displays data from an Access table in a
ListBox that has four columns. A user can click one of four RadioButtons to
select on which column the data is sorted. All works well so far.

Now, I want to change the order of the columns in the ListBox, so that the
column on which the data is sorted is always the left-most. That's where I
running into a problem.

The format and content of the ListBox is controlled by
ItemTemplate="{StaticResource PlantTemplate}". PlantTemplate is a
DataTemplate (defined as a Grid resource) like this:
<Grid
. . . .
<Grid.Resources>
<DataTemplate x:Key="PlantTemplate">
<Grid
Name="plantListGrid">
<Grid.ColumnDefinitions>
ColumnDefinition Width="150"/>
. . . .
</Grid.ColumnDefinitions>
<TextBlock
Name="textColumn0"
Text="{Binding Path=txtCommonName}"
Grid.Column="0"/>
<TextBlock
. . . .
<TextBlock
. . . .
<TextBlock
. . . .
</Grid>
</DataTemplate>
</Grid.Resources>
. . . .
</Grid>

I have tried unsuccessfully to write code-behind that changes the Text= and
Grid.Column= items in the four TextBlocks when a user clicks a RadioButton
to select the column used for sorting. I haven't been able to find a way to
access the Text and Grid.Column values from code-behind.

I have also considered having four separate DataTemplates (with different
names) and switching between templates when a user clicks a RadioButton, but
don't know how to that.

I'll appreciate any help in solving this problem. Perhaps there is a better
approach?

Gordon Padwick
 
P

Pavel Minaev

I'm creating a WPF application that displays data from an Access table ina
ListBox that has four columns. A user can click one of four RadioButtons to
select on which column the data is sorted. All works well so far.

Now, I want to change the order of the columns in  the ListBox, so thatthe
column on which the data is sorted is always the left-most. That's where I
running into a problem.

The format and content of the ListBox is controlled by
ItemTemplate="{StaticResource PlantTemplate}". PlantTemplate is a
DataTemplate (defined as a Grid resource) like this:
<Grid
    . . . .
    <Grid.Resources>
        <DataTemplate x:Key="PlantTemplate">
            <Grid
                Name="plantListGrid">
                <Grid.ColumnDefinitions>
                    ColumnDefinition Width="150"/>
                    . . . .
                </Grid.ColumnDefinitions>
                <TextBlock
                    Name="textColumn0"
                    Text="{Binding Path=txtCommonName}"
                    Grid.Column="0"/>
                <TextBlock
                . . . .
                <TextBlock
                . . . .
                <TextBlock
                . . . .
           </Grid>
        </DataTemplate>
    </Grid.Resources>
    . . . .
</Grid>

I have tried unsuccessfully to write code-behind that changes the Text=and
Grid.Column= items in the four TextBlocks when a user clicks a RadioButton
to select the column used for sorting. I haven't been able to find a way to
access the Text and Grid.Column values from code-behind.

You should use FrameworkTemplate.FindName() to find elements in a
templated control by their template names.

On a side note, ListBox is a rather weird choice to create a
multicolumn list, and your template with manual header creation
reinforces this. Why aren't you using ListView? Or, if it's a non-
interactive element (e.g. a report), Table?
I have also considered having four separate DataTemplates (with different
names) and switching between templates when a user clicks a RadioButton, but
don't know how to that.

You can just assign control's Template property from code-behind, if
you ever need to do that. But it's obviously not a very scalable
approach, as you'll need to add more templates as more columns get
added.
 
G

Gordon Padwick

Thanks, Pavel, for your fast and informative response. I'll folloiw up on
your suggestions.

Your mention of FrameworkTemplate is particularly interesting - I hadn't
come across that before. I looked, without success, for information in the
books I have. I looked also in Visual Studio Help and found some
information that is not very clear.

Regarding my use of ListBox - I just followed the suggestion in the Help
topic "How to: Bind to an ADO.NET Data Source."

I have four WPF-related books, each of which helps me understand WPF. These
are:

Microsoft Visual C# 2008 by John Sharp (published by Microsoft Press).

Windows Presentation Foundation Unleashed by Adam Nathan (published by SAMS)

Programming WPF by Chris Sells and Ian Griffiths (published by O'Reilly)

Professional C# by Christian Nagel et al (published by Wrox)

Each of these books helps me understand WPF. But each one of the books
suffers from the problem that the authors use technical terms without
defining what those terms mean. Also the books contain valuable information,
but the indexes to those books do not contain entries that refer to that
information. Many times, after carefully reading a book, I want go go back
to some information I remember reading, but can't find that place in the
book. Publishers: please do a much better job of creating indexes.

One or more of the books I have probably contains some useful information
about FrameworkTemplate. But, how I can I find that information if it's not
referred to in the index?

Can you (or anyone else) suggest a book or web site that is a good resource
for understanding terms used in the WPF environment?

Gordon Padwick


I'm creating a WPF application that displays data from an Access table in
a
ListBox that has four columns. A user can click one of four RadioButtons
to
select on which column the data is sorted. All works well so far.

Now, I want to change the order of the columns in the ListBox, so that the
column on which the data is sorted is always the left-most. That's where I
running into a problem.

The format and content of the ListBox is controlled by
ItemTemplate="{StaticResource PlantTemplate}". PlantTemplate is a
DataTemplate (defined as a Grid resource) like this:
<Grid
. . . .
<Grid.Resources>
<DataTemplate x:Key="PlantTemplate">
<Grid
Name="plantListGrid">
<Grid.ColumnDefinitions>
ColumnDefinition Width="150"/>
. . . .
</Grid.ColumnDefinitions>
<TextBlock
Name="textColumn0"
Text="{Binding Path=txtCommonName}"
Grid.Column="0"/>
<TextBlock
. . . .
<TextBlock
. . . .
<TextBlock
. . . .
</Grid>
</DataTemplate>
</Grid.Resources>
. . . .
</Grid>

I have tried unsuccessfully to write code-behind that changes the Text=
and
Grid.Column= items in the four TextBlocks when a user clicks a RadioButton
to select the column used for sorting. I haven't been able to find a way
to
access the Text and Grid.Column values from code-behind.

You should use FrameworkTemplate.FindName() to find elements in a
templated control by their template names.

On a side note, ListBox is a rather weird choice to create a
multicolumn list, and your template with manual header creation
reinforces this. Why aren't you using ListView? Or, if it's a non-
interactive element (e.g. a report), Table?
I have also considered having four separate DataTemplates (with different
names) and switching between templates when a user clicks a RadioButton,
but
don't know how to that.

You can just assign control's Template property from code-behind, if
you ever need to do that. But it's obviously not a very scalable
approach, as you'll need to add more templates as more columns get
added.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top