使用 ItemsSource 绑定的 ComboBox 会影响其他属性

发布于 2024-12-09 20:03:33 字数 1824 浏览 0 评论 0原文

我有一个组合框,显示可用系统颜色的列表。组合框中的每个项目都有一个预览矩形和文本颜色名称。在给定时间,我可能需要在屏幕上同时显示多达 6 个组合框。我创建了一个静态项目列表,以便在所有组合框中重复使用,以减少开销。它有效,而且速度很快,但现在当我更改其他组合框属性(例如将字体粗细设置为粗体)时,它会影响所有组合框,而不仅仅是我应用该属性的组合框。

这是我的代码,全部在代码后面完成。

我重用的列表和组合框的声明:

    static private List<ListViewItem> _colorItems = null;
    ComboBox _comboBoxColorList;

然后在包含组合框的控件的构造函数中,我有用于初始创建列表的代码:

        if (_colorItems == null)
        {
            _colorItems = new List<ListViewItem>();

            PropertyInfo[] colorProperties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
            Dictionary<String, Color> colorDictionary = colorProperties.ToDictionary(p => p.Name, p => (Color)p.GetValue(null, null));
            ListViewItem newItem;
            foreach (KeyValuePair<String, Color> keyPair in colorDictionary)
            {
                newItem = CreateListViewItem(keyPair.Key, keyPair.Value);
                _colorItems.Add(newItem);
            }
        }

然后创建组合框:

        _comboBoxColorList = new ComboBox();
        _comboBoxColorList.Height = Constants.ListViewPropertyComboBoxHeight;
        _comboBoxColorList.VerticalContentAlignment = VerticalAlignment.Center;
        _comboBoxColorList.Background = Brushes.White;
        _comboBoxColorList.ItemsSource = _colorItems;
        _comboBoxColorList.SelectionChanged += new SelectionChangedEventHandler(comboBoxColorList_SelectionChanged);
        Children.Add(_comboBoxColorList);

然后在事件处理程序中创建组合框有这段代码将组合框设置为粗体:

            _comboBoxColorList.FontWeight = FontWeights.Bold;

如果我这样做,使 _colorItems 不是静态的,一切都会按其应有的方式运行,但速度很慢。当 _colorItems 是静态时,速度非常快,但上面的行使所有共享项目源的组合框变为粗体。

任何洞察力或智慧都会很棒。

I have a combo box that displays a list of available system colors. Each item in the combo box has a preview rectangle and the text color name. At a given time I may need as many as 6 of these combo boxes on the screen at once. I created a static list of items to reuse across all combo boxes to reduce overhead. It works, and it is fast, but now when I change other combo box properties such as setting the font weight to bold, it affects all combo boxes, not just the one I applied the property to.

Here is my code, all done in code behind.

Declaration of the list I reuse and the combo box:

    static private List<ListViewItem> _colorItems = null;
    ComboBox _comboBoxColorList;

Then in the constructor for my control that contains the combo box I have the code for the initial creation of the list:

        if (_colorItems == null)
        {
            _colorItems = new List<ListViewItem>();

            PropertyInfo[] colorProperties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
            Dictionary<String, Color> colorDictionary = colorProperties.ToDictionary(p => p.Name, p => (Color)p.GetValue(null, null));
            ListViewItem newItem;
            foreach (KeyValuePair<String, Color> keyPair in colorDictionary)
            {
                newItem = CreateListViewItem(keyPair.Key, keyPair.Value);
                _colorItems.Add(newItem);
            }
        }

Then I create the combo box:

        _comboBoxColorList = new ComboBox();
        _comboBoxColorList.Height = Constants.ListViewPropertyComboBoxHeight;
        _comboBoxColorList.VerticalContentAlignment = VerticalAlignment.Center;
        _comboBoxColorList.Background = Brushes.White;
        _comboBoxColorList.ItemsSource = _colorItems;
        _comboBoxColorList.SelectionChanged += new SelectionChangedEventHandler(comboBoxColorList_SelectionChanged);
        Children.Add(_comboBoxColorList);

Then later one in an event handler I have this code to set the combo box to bold:

            _comboBoxColorList.FontWeight = FontWeights.Bold;

If I make it so that _colorItems is not static everythings behaves as it should, but it is slow. When _colorItems is static it is very fast, but the line above makes all combo boxes taht share the item source bold.

Any insight or wisdom would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小伙你站住 2024-12-16 20:03:33

不要直接创建 ListBoxItems,而是让框架来完成此操作。事实上,您在代码中做了很多工作,这些工作可以而且应该在 XAML 中完成。

为了简单起见:

使用您创建的新类创建一个 ObservableCollection ,将其命名为 ColorInfo 添加所有属性,例如名称、真实颜色。并用您所有的颜色填写此列表。
您可以根据需要多次重复使用。

class ColorInfo
{
    public Color Color{get;set;}
    public string Name{get;set;}
}

ObservableCollection<ColorInfo> myColors;

现在,您将此列表设置为 ComboBoxItemsSource。最后,您提供一个项目模板,每个条目应该是什么样子。

<ComboBox x:Name="_comboBoxColorList">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
            <Rectangle DockPanel.Dock="Left" Width="16" Height="16">
                <Rectangle.Fill>
                    <SolidColorBrush Color="{Binding Color}"/>
                </Rectangle.Fill>
            </Rectangle>
                <TextBlock Fill="{Binding Name}"/>
            </DockPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

当然,您可以提取模板并通过 StaticResource 引用它。

Instead of creating ListBoxItems directly, let this be done by the Framework. In fact you do alot in code which can, and should, be done in XAML.

To make it simple:

Create an ObservableCollection with a new class you create, call it maybe ColorInfo add all your properties like name, the real color. And fill this list with all your colors.
This can be reused as often as you want.

class ColorInfo
{
    public Color Color{get;set;}
    public string Name{get;set;}
}

ObservableCollection<ColorInfo> myColors;

Now, you set this List as the ItemsSource of your ComboBox. And finally you provide an item template, how each entry should look like.

<ComboBox x:Name="_comboBoxColorList">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
            <Rectangle DockPanel.Dock="Left" Width="16" Height="16">
                <Rectangle.Fill>
                    <SolidColorBrush Color="{Binding Color}"/>
                </Rectangle.Fill>
            </Rectangle>
                <TextBlock Fill="{Binding Name}"/>
            </DockPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Of course you could extract the Template and reference it via StaticResource.

小苏打饼 2024-12-16 20:03:33

如果这些 ListViewItems 是来自框架的,那么您不应该这样做。如果您有一个在控件之间共享的列表,它应该只包含数据,而不包含 UI 元素。如果您需要以某种方式显示数据,请使用 ItemTemplate 和/或 ItemContainerStyle

另请参阅:样式和模板

(只需设置 ItemsSource 即可顺便说一句,这不是绑定...)

If those ListViewItems are the ones from the framework you should not do that. If you have a list that is shared between controls it should only contain data, not UI-elements. If you need the data to be displayed in a certain way use the ItemTemplate and/or the ItemContainerStyle.

See also: Styling and Templating

(Just setting the ItemsSource is not a binding by the way...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文