Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ListBox in WPF

Tags:

c#

.net

wpf

I am trying to create a custom ListBox control in WPF for a chat Messenger. I am using an ellipse to show the online/offline user. The ellipse is to be displayed on left and some text in center of the ListBoxItem.

I want to set the ellipse fill propert to red/green based on some variable.

This is what I have done :

<ListBox Name="myList" HorizontalAlignment="Left" Height="232" Margin="117,74,0,0" VerticalAlignment="Top" Width="207">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DockPanel>
                        <Ellipse Name="ellipse" Fill="Red" DockPanel.Dock="Left">
                            <Ellipse.Triggers>
                                <Trigger Property="{Binding Online}" Value="True">
                                    <Setter TargetName="ellipse" Property="Ellipse.Fill" Value="Green"/>
                                </Trigger>
                            </Ellipse.Triggers>
                        </Ellipse>
                        <TextBlock Text="{Binding text}"></TextBlock>
                    </DockPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>            
        </ListBox>

and in the code :

myList.Items.Add(new { text="Hello",Online="True"  });

I am getting an error as

Cannot find the static member 'FillProperty' on the type 'ContentPresenter'.

What am I doing wrong here?

like image 365
Tanuj Wadhwa Avatar asked Mar 25 '26 10:03

Tanuj Wadhwa


1 Answers

Obviously this is wrong: Property="{Binding Online}"

Also you should use a Style for triggers, no need to set TargetName, and you need to take precedence into consideration, and use a Setter for the default value.

like image 95
H.B. Avatar answered Mar 28 '26 01:03

H.B.