3

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?

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

you are actually misleading WPF with some of these concerns.

  1. Binding A property on trigger will not work. you have to use DataTrigger insteed of Triggers.
  2. Implementing Trigger on the Fly for any control. most of the times not work. So go with Styles.
  3. While you are creating Ellipse in template make sure you have created enough size for it. So that can be visible to users.

try this.

<Window.Resources>
    <Style x:Key="elstyle" TargetType="Ellipse">
        <Setter Property="Height" Value="5"/>
        <Setter Property="Width" Value="5"/>
        <Setter Property="Fill" Value="Red"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Online}" Value="true">
                <Setter Property="Fill" Value="Green"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>
<Grid>
    <ListBox x:Name="myList" HorizontalAlignment="Left" Height="232" Margin="117,74,0,0" VerticalAlignment="Top" Width="207">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <Ellipse Name="ellipse" Margin="5" DockPanel.Dock="Left" Style="{DynamicResource elstyle}">
                    </Ellipse>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </DockPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

code behind .

  public MainWindow()
        {
            Random r = new Random();
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                myList.Items.Add(new { Name = "Name" + i.ToString(), Online = Convert.ToBoolean(r.Next(-1, 1)) });
            }
        }

Comments

1

You need to set the initial colour via a Setter and not in XAML. For more information and see this question: Change the color of ellipse when mouse over

Comments

0

there are a few issues in your XAML

  • set the size of your Ellipse
  • you need to use Style instead of Ellipse.Triggers
  • set your Fill color in your Style if you wane be able to change it in XAML by some condition

here an working example for your problem

            <DataTemplate>

                <!--<DockPanel> juste because i like StackPanel-->
                    <StackPanel Orientation="Horizontal">

                    <!--<Ellipse Name="ellipse" Fill="Red" DockPanel.Dock="Left">-->
                    <Ellipse Name="ellipse" Width="15" Height="15">

                        <!--<Ellipse.Triggers>-->
                        <Ellipse.Style>
                            <Style TargetType="Ellipse">      

                                <Setter Property="Fill" Value="Red"/>

                                <Style.Triggers>

                                    <!--<Trigger Property="{Binding Online}" Value="True">-->
                                    <DataTrigger Binding="{Binding Online}" Value="True">
                                        <Setter Property="Fill" Value="LimeGreen"/>
                                    </DataTrigger>

                                </Style.Triggers>

                            </Style>
                        </Ellipse.Style>

                    </Ellipse>
                    <TextBlock Text="{Binding text}"/>

                </StackPanel>
            </DataTemplate>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.