3

我正在尝试在 WPF 中为聊天 Messenger 创建一个自定义 ListBox 控件。我正在使用椭圆来显示在线/离线用户。椭圆将显示在左侧,一些文本将显示在 ListBoxItem 的中心。

我想根据一些变量将椭圆填充属性设置为红色/绿色。

这就是我所做的:

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

并在代码中:

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

我收到一个错误

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

我在这里做错了什么?

4

4 回答 4

4

显然这是错误的:Property="{Binding Online}"

另外你应该使用aStyle作为触发器,不需要设置TargetName,你需要考虑优先级,并使用aSetter作为默认值。

于 2013-06-20T04:16:41.413 回答
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)) });
            }
        }
于 2013-06-20T06:15:05.050 回答
1

您需要通过 Setter 而不是在 XAML 中设置初始颜色。有关更多信息,请参阅此问题:鼠标悬停时更改椭圆的颜色

于 2013-06-20T04:26:40.000 回答
0

您的 XAML 中存在一些问题

  • 设置你的大小Ellipse
  • 你需要使用Style而不是Ellipse.Triggers
  • 如果您希望能够通过某些条件在 XAML 中更改它,请Fill在您的颜色中设置您的颜色Style

这是您问题的一个工作示例

            <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>
于 2013-06-20T06:12:09.027 回答