简体   繁体   English

WPF-组合框C#选择未显示在活动窗口中

[英]WPF - combobox c# selection is not displayed on active window

I create an WPF app in VS 2015 express environment. 我在VS 2015 Express环境中创建了WPF应用。 Right now I'm struggling with combobox value presentation problem. 现在,我正在努力解决组合框值表示问题。 My combobox looks like that: 我的组合框如下所示:

<ComboBox 
    x:Name="cb_pers_ucz"  
    ItemsSource="{Binding Path=Mechanizmy.GlobalObj.SLO_PER_UCZ}" 
    DisplayMemberPath="nazwa" 
    SelectedValuePath="id_poz" 
    IsSynchronizedWithCurrentItem="True" 
    SelectedValue="{Binding Mechanizmy.GlobalObj.SLO_PER_UCZ, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
    ToolTip="" 
    HorizontalAlignment="Left" 
    Margin="175,127,0,0" 
    VerticalAlignment="Top" 
    Width="185" 
    Height="22" 
    Grid.Row="1" 
    ToolTipOpening="cb_pers_ucz_ToolTipOpening"
    />

Everything works fine till I need to set selected value via c# code. 一切正常,直到我需要通过C#代码设置选定的值。 When I use SelectedIndex or SelectedValue property combobox is changing ID value to selected but there's blank value on window (GUI). 当我使用SelectedIndex或SelectedValue属性时,组合框会将ID值更改为selected,但是窗口(GUI)上的值为空白。

I'm using List "SLO_PER_UCZ" to fill combobox. 我正在使用列表“ SLO_PER_UCZ”填充组合框。 It looks like that: 看起来像这样:

    public class slowniki
    {
        public int id_poz { get; set; }
        public string nazwa { get; set; }
        public string definicja { get; set; }
        public DateTime dt_od { get; set; }
        public DateTime dt_do { get; set; }
    }

    public static List<slowniki> SLO_PER_UCZ = new List<slowniki>();

Sorry for quality of Combobox structure, but I was trying to implement several solutions that I found on stackoverflow and other portals (with no effect). 对Combobox结构的质量感到抱歉,但是我试图实现在stackoverflow和其他门户网站上发现的几种解决方案(没有效果)。

Mechanizmy.GlobalObj.SLO_PER_UCZ is the collection. Mechanizmy.GlobalObj.SLO_PER_UCZ是集合。 It can't be the selected value as well. 也不能是所选值。 Bind SelectedValue to a different property that will take the selected value. SelectedValue绑定到将采用所选值的其他属性。

Since you want to set it as well, I'm also showing you how to implement INotifyPropertyChanged on your viewmodel. 由于您也想设置它,因此我还将向您展示如何在视图模型上实现INotifyPropertyChanged

public class GlobalObjClassName : INotifyPropertyChanged
{

    //  ... etc. etc. etc. ...
    public static List<slowniki> SLO_PER_UCZ = new List<slowniki>();

    private int _selectedIDPoz = -1;
    public int SelectedIDPoz
    {
        get { return _selectedIDPoz; }
        set
        {
            if (value != _selectedIDPoz)
            {
                _selectedIDPoz = value;
                OnPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] String propName = null)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));

XAML: XAML:

<ComboBox 
    x:Name="cb_pers_ucz"  
    ItemsSource="{Binding Path=Mechanizmy.GlobalObj.SLO_PER_UCZ}" 
    DisplayMemberPath="nazwa" 
    SelectedValuePath="id_poz" 
    IsSynchronizedWithCurrentItem="True" 
    SelectedValue="{Binding Mechanizmy.GlobalObj.SelectedIDPoz}" 
    ToolTip="" 
    HorizontalAlignment="Left" 
    Margin="175,127,0,0" 
    VerticalAlignment="Top" 
    Width="185" 
    Height="22" 
    Grid.Row="1" 
    ToolTipOpening="cb_pers_ucz_ToolTipOpening"
    />

And don't use UpdateSourceTrigger=PropertyChanged or Mode=TwoWay until you find out what they mean. 除非您了解它们的含义,否则不要使用UpdateSourceTrigger=PropertyChangedMode=TwoWay Adding random stuff you don't understand just wastes your time and gets you nowhere. 添加您不了解的随机内容只会浪费您的时间,使您无所适从。

Thanks a lot for Your answer. 非常感谢您的回答。 It wasn't strick answer to My question but gave me enough information to solve problem. 这不是我问题的正确答案,但给了我足够的信息来解决问题。

Once more thanks :) 再次感谢:)

Kind Regards Sebastian 亲切的问候塞巴斯蒂安

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

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