简体   繁体   English

WPF Combobox 与 object 的绑定问题

[英]WPF Combobox binding issue with object

I have found several example of wpf combobox binding but when trying to adapt it I don't get the expected results.我找到了 wpf combobox 绑定的几个示例,但是在尝试调整它时我没有得到预期的结果。 What appears in the combobox instead of the value is this: CuttingProblemHelper.Models.ComboBoxPairs combobox 中出现的不是值,而是:CuttingProblemHelper.Models.ComboBoxPairs

Can please anyone help?可以请任何人帮忙吗?

Example of data in the SQL table: SQL表中的数据示例:

Id CutProbCategId Problem Id CutProbCategId 问题

1 1 Brins coupés 1 1 布林斯双门轿跑车

2 1 Brins grafignés 2 1 布林斯涂鸦

3 1 Fil non dégainé 3 1 Fil non dégainé

What I try to acheive is having a key, value pair for each entry in the combobox from an sql table.我试图实现的是从 sql 表中的 combobox 中的每个条目都有一个键值对。

So I created a class to store key, value pair:所以我创建了一个 class 来存储键值对:

namespace CuttingProblemHelper.Models {

public class ComboBoxPairs
{
    public int _Key { get; set; }
    public string _Value { get; set; }

    public ComboBoxPairs(int _key, string _value)
    {
        _Key = _key;
        _Value = _value;
    }
} }

Next get the data from the table and add them to the object ComboBoxPairs接下来从表中获取数据并将它们添加到 object ComboBoxPairs

private void AddProblemCategtoCombobox(int categ)
    {
        // erase list
        cmbxProblem.ItemsSource = null;

        // get list
        DataRow[] CutProblemsRows = gediDataSet.CutProblems.Select("CutProbCategId= " + categ);
        // we have rows
        if (CutProblemsRows != null)
        {
            // initialize object ComboBoxPairs
            List<ComboBoxPairs> cbp = new List<ComboBoxPairs>();
            foreach (DataRow row in CutProblemsRows)
            {
                // add id and problem key, value pair
                cbp.Add(new ComboBoxPairs(int.Parse(row["Id"].ToString()), row["Problem"].ToString()));
            }
            // define properties of combobox
            cmbxProblem.DisplayMemberPath = "_Value";
            cmbxProblem.SelectedValuePath = "_Key";
            // bind comboboxpairs list
            cmbxProblem.ItemsSource = cbp.ToList(); 
        }
    }

Here's the XAML of the combobox and formatting of the display of items:这是 combobox 的 XAML 和项目显示的格式:

<ComboBox x:Name="cmbxProblem" Grid.Column="1" Margin="10,10,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="cmbxProblem_SelectionChanged" FontSize="40" ItemsSource="{Binding Collection}" Grid.ColumnSpan="2">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <Label   Name="lbl" Content="{Binding}" ></Label>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsHighlighted" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                        <Setter TargetName="lbl" Property="Background" Value="AliceBlue"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Italic"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

Thank you very much!非常感谢!

I found a solution but the combobox does not only display the value but it displays the paired key and value.我找到了一个解决方案,但 combobox 不仅显示值,而且显示配对的键和值。 But when a value is selected it displays only the selected value. But when a value is selected it displays only the selected value. Anyone knows what to change to show only the value without the key?任何人都知道要更改什么以仅显示没有密钥的值?

See pictures to better understand.查看图片以更好地理解。 组合框中的项目 选择的值

Here's the solution I came up with after searching several combobox solutions: In the XAML这是我在搜索了几个 combobox 解决方案后得出的解决方案:在 XAML

<ComboBox x:Name="cmbxProblem" Grid.Column="1" Margin="10,10,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="cmbxProblem_SelectionChanged" FontSize="40" ItemsSource="{Binding MyCollection}" DisplayMemberPath="_Value" SelectedValuePath="_Key" Grid.ColumnSpan="2">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <Label   Name="lbl" Content="{Binding}" ></Label>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsHighlighted" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                        <Setter TargetName="lbl" Property="Background" Value="AliceBlue"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Italic"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

In the code behind:在后面的代码中:

 private void AddProblemCategtoCombobox(int categ)
    {
        // delete list
        cmbxProblem.ItemsSource = null;

        // get items for list
        DataRow[] CutProblemsRows = gediDataSet.CutProblems.Select("CutProbCategId= " + categ);
        // we have items
        if (CutProblemsRows != null)
        {
            // create collection
            MyCollection = new ObservableCollection<KeyValuePair<int, string>>();

            foreach (DataRow row in CutProblemsRows)
            {
                // fill collection with items
                MyCollection.Add(new KeyValuePair<int, string>(int.Parse(row["Id"].ToString()), row["Problem"].ToString()));
            }
            // define properties for the combobox
            cmbxProblem.DisplayMemberPath = "Value";
            cmbxProblem.SelectedValuePath = "Key";
            cmbxProblem.ItemsSource = MyCollection; 
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM