C# 这是为什么;显然是",;假条件返回真?

C# 这是为什么;显然是",;假条件返回真?,c#,wpf,boolean,C#,Wpf,Boolean,在我的测试场景中,所有组合框都显示相同的值(“”) private bool AtLeastOnePlatypusSelected() { 字符串DefaultPlatypusValue=“”; 返回(cmbxWeek1.SelectedValue!=默认PlatypusValue)|| (cmbxWeek2.SelectedValue!=默认PlatypusValue)|| (cmbxWeek3.SelectedValue!=默认PlatypusValue)|| (cmbxWeek4.Sele

在我的测试场景中,所有组合框都显示相同的值(“”)

private bool AtLeastOnePlatypusSelected()
{
字符串DefaultPlatypusValue=“”;
返回(cmbxWeek1.SelectedValue!=默认PlatypusValue)||
(cmbxWeek2.SelectedValue!=默认PlatypusValue)||
(cmbxWeek3.SelectedValue!=默认PlatypusValue)||
(cmbxWeek4.SelectedValue!=默认PlatypusValue)||
(cmbxWeek5.SelectedValue!=默认PlatypusValue)||
(cmbxWeek6.SelectedValue!=默认PlatypusValue)||
(cmbxWeek7.SelectedValue!=DefaultPlatypusValue)||
(cmbxWeek8.SelectedValue!=默认PlatypusValue)||
(cmbxWeek9.SelectedValue!=DefaultPlatypusValue);
}
…但此函数正在返回true

下面是另一个组合框代码:

public ObservableCollection<ComboBoxItem> cbItems { get; set; }
public ComboBoxItem SelectedcbItem { get; set; }
private Dictionary<int, int> PointNumToWeekNumPairings = new Dictionary<int, int>();

public MainWindow()
{
    InitializeComponent();
    DataContext = this;

    cbItems = new ObservableCollection<ComboBoxItem>();
    var cbItem = new ComboBoxItem { Content = "<--Select-->" };
    SelectedcbItem = cbItem;
    cbItems.Add(cbItem);
    cbItems.Add(new ComboBoxItem { Content = "Tony Iommi" });
    cbItems.Add(new ComboBoxItem { Content = "Mike McCarthy" });
    cbItems.Add(new ComboBoxItem { Content = "Micah Profit" });
    cbItems.Add(new ComboBoxItem { Content = "Allan Poe" });
    cbItems.Add(new ComboBoxItem { Content = "Bill Bailey" });
    cbItems.Add(new ComboBoxItem { Content = "Duane Eddy" });
    cbItems.Add(new ComboBoxItem { Content = "John Kennedy" });
    cbItems.Add(new ComboBoxItem { Content = "Bert Erneson" });
    cbItems.Add(new ComboBoxItem { Content = "Clyde Valouch" });
    cbItems.Add(new ComboBoxItem { Content = "Micky Thompson" });
}
publicobservableCollection cbItems{get;set;}
公共ComboBoxItem SelectedcbItem{get;set;}
私有字典点numtowekenumpairings=新字典();
公共主窗口()
{
初始化组件();
DataContext=this;
cbItems=新的ObservableCollection();
var cbItem=新ComboBoxItem{Content=”“};
SelectedcbItem=cbItem;
cbItems.Add(cbItem);
添加(新ComboBoxItem{Content=“Tony Iommi”});
添加(新的ComboBoxItem{Content=“Mike McCarthy”});
添加(新ComboBoxItem{Content=“Micah profice”});
添加(新ComboBoxItem{Content=“Allan Poe”});
添加(新ComboBoxItem{Content=“Bill Bailey”});
添加(新ComboBoxItem{Content=“Duane Eddy”});
添加(新ComboBoxItem{Content=“John Kennedy”});
添加(新ComboBoxItem{Content=“Bert ernson”});
添加(新ComboBoxItem{Content=“Clyde Valouch”});
添加(新的ComboBoxItem{Content=“Micky Thompson”});
}
查尔斯·狄更斯到底在干什么

试试这个:

private bool AtLeastOnePlatypusSelected()
{
    string DefaultPlatypusValue = "<--Select-->";
    return (!cmbxWeek1.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek2.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek3.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek4.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek5.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek6.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek7.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek8.SelectedValue.Equals(DefaultPlatypusValue) ||
       (cmbxWeek9.SelectedValue.Equals(DefaultPlatypusValue));
}
private bool AtLeastOnePlatypusSelected()
{
字符串DefaultPlatypusValue=“”;
返回(!cmbxWeek1.SelectedValue.Equals(DefaultPlatypusValue)||
(cmbxWeek2.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek3.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek4.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek5.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek6.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek7.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek8.SelectedValue.Equals(默认PlatypusValue)||
(cmbxWeek9.SelectedValue.Equals(DefaultPlatypusValue));
}
通常,您希望使用“Equals”方法测试两个对象的“equalness”

在C#中,“==”比较更类似于测试两个对象是否是相同的实例,这是您很少会做的事情(在您的示例中,显然不是您想要的)


编辑:原来,C#中的字符串并非如此。

使用组合框的
数组或
列表:

var cmbxWeeks = new List<ComboBox>();
cmbxWeeks.Add(cmbxWeek1 ...
查尔斯·狄更斯到底在干什么

显然,一个或多个组合框的
SelectedValue
与您预期的不同


在调试器中单步执行代码,或打印出值。

如果您的示例是Winforms示例,则是包含ValueMember属性指定的数据源成员值的对象。如果未分配ValueMember,请改用
SelectedItem
属性


请注意,WPF中的SelectedValue属性和SelectedItem属性返回ComboBoxItem对象,因此您正在将
ComboxItem
与字符串进行比较。请尝试将比较更改为类似以下内容:

private bool AtLeastOnePlatypusSelected()
{

    string DefaultPlatypusValue = "<--Select-->";
    return (string)((ComboBoxItem)cmbxWeek1.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek2.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek3.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek4.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek5.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek6.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek7.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek8.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek9.SelectedValue).Content != DefaultPlatypusValue;
} 
private bool AtLeastOnePlatypusSelected()
{
字符串DefaultPlatypusValue=“”;
返回(字符串)((ComboBoxItem)cmbxWeek1.SelectedValue).Content!=DefaultPlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek2.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek3.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek4.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek5.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek6.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek7.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek8.SelectedValue).Content!=默认PlatypusValue||
(字符串)((ComboBoxItem)cmbxWeek9.SelectedValue).Content!=DefaultPlatypusValue;
} 

尝试SelectedItem而不是SelectedValue

我编写了一个快速的小测试程序,发现组合框的SelectedValue等于null,但SelectedItem包含我期望的文本,并返回false

(抱歉,我的测试是WinForms…)


好的,我的新测试显示SelectionBoxItem将返回字符串,并应提供您想要的结果。

编辑:正如我所想,您可以手动创建
ListBoxItems

所选值是一个
ListBoxItem
,当然它不等于一个
string
,将
SelectedValuePath
设置为
“Content”
,或者直接删除
ListBoxItems
并添加字符串,控件将为您创建容器(这通常是首选,数据集合中不应该有UI元素,它还允许虚拟化)


所有组合框都显示相同的值(“”)

Display!=值,有一些属性定义显示的内容和选择的内容。 e、 g.如果您有数据库条目,您可能希望将作为主键(某个整数),因此您可以相应地设置,
private bool AtLeastOnePlatypusSelected()
{
    string DefaultPlatypusValue = "<--Select-->";

    bool result = true;
    foreach (var cmbxWeek in cmbxWeeks)
        result = result  || (cmbxWeek.SelectedValue != DefaultPlatypusValue);

    return result;
}
return cmbxWeeks.Aggregate(true, (current, cmbxWeek) => current || ((string) cmbxWeek.SelectedValue != DefaultPlatypusValue));
private bool AtLeastOnePlatypusSelected()
{

    string DefaultPlatypusValue = "<--Select-->";
    return (string)((ComboBoxItem)cmbxWeek1.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek2.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek3.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek4.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek5.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek6.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek7.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek8.SelectedValue).Content != DefaultPlatypusValue ||
           (string)((ComboBoxItem)cmbxWeek9.SelectedValue).Content != DefaultPlatypusValue;
} 
return ((!(cmbxWeek1.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek2.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek3.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek4.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek5.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek6.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek7.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek8.SelectedValue.ToString().Contains(DefaultPlatypusValue))) ||
        (!(cmbxWeek9.SelectedValue.ToString().Contains(DefaultPlatypusValue))));