C# .NET - C# WPF Foreach ListViewItem cast as Object

Asked By Garry Cooper on 28-Jun-09 12:14 PM

Hi,

i´ve try to highlight some nullvalues in a listview(datatemplate=textboxes). The ListView is named "listViewBlock" containing my Object(FDBlock) wich has several items. How I can cast this ListViewItem as my FDBlock Object?

private FDBlock _fdBlockSelected;

void listViewBlock_Loaded(object sender, RoutedEventArgs e) {
            foreach(ListViewItem item in listViewBlock.Items) {
                if(item.Content == null)
                    item.Background = Brushes.Red;
            }
        }

Wouldn't you use a DataTemplate.Trigger for that?

Robbe Morris replied to Garry Cooper on 28-Jun-09 06:13 PM

I ripped this out of an old WPF application DataTemplate as a syntax example.  I'd think you'd set up a Trigger and react accordingly. 

<DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=YourProperty}" Value="True">
                    <Setter TargetName="???" Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=YourProperty}" Value="False">
                    <Setter TargetName="???" Property="Visibility" Value="{Binding Path=RelatedExpandableRange.Visibility}"/>
                </DataTrigger>
            </DataTemplate.Triggers>

RE

Ravenet Rasaiyah replied to Garry Cooper on 28-Jun-09 11:18 PM
Hi

It's quite easy man


private FDBlock _fdBlockSelected;

void listViewBlock_Loaded(object sender, RoutedEventArgs e) {

foreach(ListViewItem item in listViewBlock.Items) {

_fdBlockSelected=(FDBlock) item;
if(item.Content == null)
item.Background = Brushes.Red;
}

}

Here my article about listview custom object bind in WPF
http://www.codegain.com/index.php?option=com_content&view=article&id=160:work-with-listview-in-wpf&catid=162:controls&Itemid=164

Thank you
http://www.codegai.com

Re. cast ListViewItem as Object

Garry Cooper replied to Ravenet Rasaiyah on 29-Jun-09 02:42 AM
Hi,
mh if i compile this this the debugger says that a conversion from ListViewItem in FDBlock isn`t possible.
Why that?
 
 
void listViewBlock_Loaded(object sender, RoutedEventArgs e) {
            foreach(ListViewItem item in listViewBlock.Items) {
                _fdBlockSelected = (FDBlock) item;
                if(item.Content == null)
                    item.Background = Brushes.CornflowerBlue;
            }
        }
OK great
Ravenet Rasaiyah replied to Garry Cooper on 29-Jun-09 02:43 AM
Hi

How you are binding to listview? can you post bind code here, then it's easy to solve

Thank you
http://www.codegain.com
Re.: cast Object as ListViewItem (Mark null values with color)
Garry Cooper replied to Robbe Morris on 29-Jun-09 03:04 AM

Hi Robbe,

mh i´ve never worked with a datatrigger. I dont understand how you could ask for a null value with a trigger?!

Re.:
Garry Cooper replied to Ravenet Rasaiyah on 29-Jun-09 03:21 AM
Hi,
i bind this through ItemsSource. Thanx for the Help!:-)

listViewBlock.ItemsSource = FDBlock.GetFDBlockList(......


xaml:

<GridViewColumn Header="Image" 
                    Width="auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>  
                                      <TextBox                                      
                                         Text="{Binding Path=Image}"
                                         Height="30"
                                         Background="Transparent"
                                         HorizontalContentAlignment="Left"
                                         VerticalContentAlignment="Bottom"
                                         Margin="-6,0,-9,0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
RE
Ravenet Rasaiyah replied to Garry Cooper on 29-Jun-09 03:40 AM
Hi

this method return collectipon of  FDBlock is it?
 FDBlock.GetFDBlockList(...... 

Then our cast is fine , anyhow try on this way also.

DataRowView viewdata=lisviewdb.Items;

thank you
http://www.codegain.com
Value="{x:Null}"
Robbe Morris replied to Garry Cooper on 29-Jun-09 10:32 AM
end of post
Garry Cooper replied to Robbe Morris on 30-Jun-09 01:56 AM

Hi,

i´ve took your solution and its work! THANX! :-)