WPF ComboBox SelectedItem [英] WPF ComboBox SelectedItem

查看:142
本文介绍了WPF ComboBox SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ok一直在使用WPF一段时间,但我需要一些帮助。



我有一个 ComboBox

 < TabControl> 
< TabItem Header =1>
< ComboBox ItemsSource ={Binding MyList}SelectedItem ={Binding MyListSelection}/>
< / TabItem>
< TabItem Header =2/>
< / TabControl>

每当我离开标签1,然后回到它时,选择将被删除。我认为这是因为控件被销毁时,他们超出范围,然后回来。但在该过程中,SelectedItem变为null,这不是真正的用户想要的,它是一个事件,由于UI生命周期。



所以我想知道什么是最好的路线?我正在用MVVM构建这个应用程序,所以我可以忽略在我的ViewModel中的MyListSelection属性的集合调用,但我有ComboBoxes所有的地方,不喜欢修改我的ViewModel我认为WPF的错误。



我可以继承WPF ComboBox,但是没有SelectedItemChanging事件我只能在SelectedItem更改时添加处理程序。



有任何想法吗?



更新:



好吧,为什么我的问题不能被复制。如果列表项类型是一个类,由于某种原因,SelectedItem被WPF设置为null,但如果它是一个值类型它不是。



这里是我的测试类VMBase只是一个实现INotifyPropertyChanged的抽象类):

  public class TestListViewModel:VMBase 
{
public TestListViewModel()
{
TestList = new List< TestViewModel>();
for(int i = 0; i <10; i ++)
{
TestList.Add(new TestViewModel(i.ToString()));
}
}

public List< TestViewModel> TestList {get;组; }

TestViewModel _SelectedTest;
public TestViewModel SelectedTest
{
get {return _SelectedTest; }
set
{
_SelectedTest = value;
OnPropertyChanged(SelectedTest);
}
}
}

public class TestViewModel:VMBase
{
public string Name {get; set;}
}

所以当我改变TestList为int类型,并在标签之间来回切换SelectedItem保持不变。但是当它的类型 TestViewModel 当Tabitem失去焦点时,SelectedTest设置为null。




解决方案

我有完全相同的问题,直到现在我无法确定问题是什么。我测试了4个不同的机器与相同的操作系统,.Net版本和硬件规格,可以重现的问题在其中两个,在其他工作很好。
解决方法我可以发现,对我来说,是在ItemsSource之前定义SelectedItem绑定。奇怪的是,如果我遵循这种模式,一切正常工作。
这就是说,你只需要做以下:

 < Window x:Class =ComboBoxInTabItemSpike.Window1 
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =Window1Height =300Width =300>
< StackPanel>
< TabControl>
< TabItem Header =1>
< ComboBox SelectedItem ={Binding MySelect}ItemsSource ={Binding MyList}/>
< / TabItem>
< TabItem Header =2/>
< / TabControl>
< TextBlock Text ={Binding MySelect}/>
< / StackPanel>
< / Window>


Ok been working with WPF for a while but I need some help.

I have a ComboBox like below:

<TabControl>
    <TabItem Header="1">
        <ComboBox ItemsSource="{Binding MyList}" SelectedItem="{Binding MyListSelection}"/>
    </TabItem>
    <TabItem Header="2"/>
</TabControl>

Whenever I move away from tab 1 and then come back to it the selection gets removed. I think the reason for that is that the controls get destroyed when they go out of scope and then back in. But in the process of that the SelectedItem becomes null which isn't really what the user wanted, it's an event due to the UI lifecycle.

So I'm wondering what is the best route to take? I'm building this app with MVVM so I could ignore a set call on the MyListSelection Property in my ViewModel but I have ComboBoxes all over the place and don't like modifying my ViewModel for what I consider a bug of WPF.

I could subclass the WPF ComboBox, but there is no SelectedItemChanging event I can only add a handler when SelectedItem changed.

Any ideas?

UPDATE:

Okay, after beating my head against the wall I found out why my problem couldn't get reproduced. If the list item type is a class for some reason the SelectedItem gets set by WPF to null but if it's a value type it doesn't.

here's my test class(VMBase is just an abstract class that implements INotifyPropertyChanged):

public class TestListViewModel : VMBase
{
    public TestListViewModel()
    {
        TestList = new List<TestViewModel>();
        for (int i = 0; i < 10; i++)
        {
            TestList.Add(new TestViewModel(i.ToString()));
        }
    }

    public List<TestViewModel> TestList { get; set; }

    TestViewModel _SelectedTest;
    public TestViewModel SelectedTest
    {
        get { return _SelectedTest; }
        set
        {
            _SelectedTest = value;
            OnPropertyChanged("SelectedTest");
        }
    }
}

public class TestViewModel : VMBase
{
  public string Name {get;set;}
}

So when I change TestList to type int and go back and forth between tabs SelectedItem stays the same. But when it is of type TestViewModel SelectedTest gets set to null when the tabitem goes out of focus.

What's going on?

解决方案

I've the exact same problem, and till now I couldn't figure what the problem is. I tested in 4 different machines with the same OS, .Net version and hardware specifications and could reproduce the issue in two of them, in the other ones worked just fine. The workaround I could find that works for me is to define the SelectedItem binding before the ItemsSource. Strangely if I follow this pattern, everything works as expected. That said, you just have to do the following:

<Window x:Class="ComboBoxInTabItemSpike.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TabControl>
            <TabItem Header="1">
                <ComboBox SelectedItem="{Binding MySelect}" ItemsSource="{Binding MyList}"/>
            </TabItem>
            <TabItem Header="2"/>
        </TabControl>
        <TextBlock Text="{Binding MySelect}"/>
    </StackPanel>
</Window>

这篇关于WPF ComboBox SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆