2011-04-22 62 views
2

我在WPF中使用了一个ListBox,并且我想仅在实际选择ListBox中的某个项目时启用/禁用按钮。WPF Listbox:HasItemSelected事件是否存在?

问题是我的按钮做了一个检索项目名称的动作,并且自初始化以来没有选择任何项目(我想保持这种方式),我得到一个错误,因为我'米空物体进行逻辑...

我真的环顾四周,我无法找到一个=/

一个愉快的一天有=)

+0

一些源代码会很好 – 2011-04-22 13:50:43

回答

1

做出value converter并绑定按钮IsEnabled到使用转换器的ListboxSelectedIndex

public class MyConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
    int ndx = (int)value; 
    if (ndx < 0) return false; 
    return true; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
    return null; 
    } 
} 


<Window.Resources> 
    <my:MyConverter x:Name="MyConverter"/> 
</Window.Resources> 

<ListBox x:Name="MyListBox"></ListBox> 
<Button IsEnabled="{Binding Path=SelectedIndex, ElementName=MyListBox, Converter={StaticResource MyConverter}}"/> 
+0

你能举个例子吗?我对价值转换器不熟悉=/ – 2011-04-22 13:56:23

+0

@FatalBaboon,我也在上面:)也请查看Switch On The Code文章的链接。 – 2011-04-22 14:02:08

+0

为什么你的答案中有一个组合框?我正在使用一个ListBox。 – 2011-04-22 14:02:22

相关问题