这两个绑定有什么区别?

时间:2021-10-04 15:49:56

Long ago, I looked at this MSDN walkthrough. It implements a color editor, and the tooltip uses the color's name like "Red" or "Blue".

很久以前,我看了这个MSDN演练。它实现了颜色编辑器,工具提示使用颜色的名称,如“红色”或“蓝色”。

Today, I'm implementing a ListBox that is similar; it displays a box with the color and the name of the color next to it. Except in my application, all of the color names display as the hex values like #FFFF0000 and #FF0000FF. Why?

今天,我正在实现类似的ListBox;它会显示一个框,其中包含颜色和旁边颜色的名称。除了在我的应用程序中,所有颜色名称都显示为十六进制值,如#FFFF0000和#FF0000FF。为什么?

Here's the ColorsList class used in both projects:

这是两个项目中使用的ColorsList类:

public ColorsList()
{
    Type type = typeof(Colors);
    foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Static))
    {
        if (propertyInfo.PropertyType == typeof(Color))
        {
            Add((Color)propertyInfo.GetValue(null, null));
        }
    }
}

This XAML snippet makes the tooltip use the color name in the MSDN project (you can see the rest of the code in the walkthrough):

此XAML代码段使工具提示使用MSDN项目中的颜色名称(您可以在演练中看到其余代码):

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button Tag="{Binding}" Command="{x:Static PropertyEditing:PropertyValueEditorCommands.ShowInlineEditor}">
            <Button.Template>
                <ControlTemplate>
                    <Border Width="30" Height="30" BorderBrush="Black" BorderThickness="1" CornerRadius="5">
                        <Rectangle Width="22" Height="22" ToolTip="{Binding}">
                            <Rectangle.Fill>
                                <SolidColorBrush Color="{Binding}"/>
                            </Rectangle.Fill>
                        </Rectangle>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </DataTemplate>
</ItemsControl.ItemTemplate>

Here's my XAML that produces the hex codes:

这是我生成十六进制代码的XAML:

<ListBox x:Name="lstColors" Grid.Row="1" ItemsSource="{StaticResource colors}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Stroke="Black"
                           StrokeThickness="3"
                           Width="24"
                           Height="24"
                           RadiusX="5"
                           RadiusY="5">
                    <Rectangle.Fill>
                        <SolidColorBrush Color="{Binding}" />
                    </Rectangle.Fill>
                </Rectangle>
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

It looks the same to me; what's the difference?

对我来说看起来一样;有什么不同?

2 个解决方案

#1


Not exactly an answer to your question, more of an observation:

不完全是你的问题的答案,更多的是观察:

If you change ColorsList to inherit from ObservableCollection<string> instead of ObservableCollection<Color> and modify the following line:

如果您将ColorsList更改为继承自ObservableCollection 而不是ObservableCollection 并修改以下行:

Add((Color)propertyInfo.GetValue(null, null));

to

Add(propertyInfo.Name);

your code will work as you want it to work. Instead of converting Color to color name, this is going the other way around - using the built-in conversion from color name to Color.

您的代码将按您希望的方式工作。而不是将Color转换为颜色名称,这是相反的 - 使用从颜色名称到Color的内置转换。

As a side note, I tried the XAML code from the MSDN sample (only the part you're citing) but the tooltips display color codes and not color names.

作为旁注,我尝试了MSDN示例中的XAML代码(只是您引用的部分),但工具提示显示颜色代码而不是颜色名称。

#2


It turns out design-time is different from run-time. The MSDN example is used as an extended property editor. I took a friend's advice and tried exposing it as a runtime control, and magically it's getting hash names for colors instead of friendly names. Neat.

事实证明,设计时与运行时不同。 MSDN示例用作扩展属性编辑器。我接受了朋友的建议并尝试将其作为运行时控件公开,并且奇怪的是它获取颜色的哈希名称而不是友好名称。整齐。

#1


Not exactly an answer to your question, more of an observation:

不完全是你的问题的答案,更多的是观察:

If you change ColorsList to inherit from ObservableCollection<string> instead of ObservableCollection<Color> and modify the following line:

如果您将ColorsList更改为继承自ObservableCollection 而不是ObservableCollection 并修改以下行:

Add((Color)propertyInfo.GetValue(null, null));

to

Add(propertyInfo.Name);

your code will work as you want it to work. Instead of converting Color to color name, this is going the other way around - using the built-in conversion from color name to Color.

您的代码将按您希望的方式工作。而不是将Color转换为颜色名称,这是相反的 - 使用从颜色名称到Color的内置转换。

As a side note, I tried the XAML code from the MSDN sample (only the part you're citing) but the tooltips display color codes and not color names.

作为旁注,我尝试了MSDN示例中的XAML代码(只是您引用的部分),但工具提示显示颜色代码而不是颜色名称。

#2


It turns out design-time is different from run-time. The MSDN example is used as an extended property editor. I took a friend's advice and tried exposing it as a runtime control, and magically it's getting hash names for colors instead of friendly names. Neat.

事实证明,设计时与运行时不同。 MSDN示例用作扩展属性编辑器。我接受了朋友的建议并尝试将其作为运行时控件公开,并且奇怪的是它获取颜色的哈希名称而不是友好名称。整齐。