【问题标题】:Xaml - combo box - why does selectedValue NOT work with multiple columns?Xaml - 组合框 - 为什么 selectedValue 不适用于多列?
【发布时间】:2013-08-13 00:34:17
【问题描述】:

您好,感谢您的帮助。

以下 xaml 可以正常工作:

<ComboBox Name="cboCit_type"
  IsSynchronizedWithCurrentItem="True"    
  mvvm:View.FlowsWithPrevious="True"
  ItemsSource="{Binding Path=cuCodeInfo.CitTypes}"
  SelectedValuePath="code" 
  DisplayMemberPath="code" 
  Text="{Binding cit_type}"
  IsEditable="true"
  IsReadOnly="false"
  SelectedValue="{Binding Path=cit_type}">
</ComboBox>

cuCodeInfo.CitTypes 只是一个可用项目的列表。有许多公共属性,但有问题的 2 个是“代码”和“描述”。

现在,我显示可用的代码值,用户选择一个。如果已经选择了一个,那么它会在页面显示时显示。这一切都很好。

然后我认为同时显示代码和描述可能会很好。我觉得应该不会太难...

所以我删除了 DisplayMemberPath 语句并添加到 ItemTemplate 中。

当我这样做时,一切看起来都很棒,直到我尝试从列表中选择一个项目。当我这样做时,我会得到一个空字符串,而不是显示选定的代码。我已经搜索了互联网,试图找到我需要添加到 DataTemplate 来解决这个问题的一件事,但是我尝试过的一切都失败了。这是不工作的代码:

<ComboBox Name="cboCit_type"
  IsSynchronizedWithCurrentItem="True"    
  mvvm:View.FlowsWithPrevious="True"
  ItemsSource="{Binding Path=cuCodeInfo.CitationTypes}"
  SelectedValuePath="code" 
  Text="{Binding cit_type}"
  IsEditable="true"
  IsReadOnly="false"
  SelectedValue="{Binding cit_type}">

  <ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" >
            <Border BorderThickness="0,0,1,0" BorderBrush="Black">
                <TextBlock Text="{Binding Path=code}" mvvm:View.WidthEx="2" ></TextBlock>
            </Border>
            <TextBlock Text="{Binding Path=description}" mvvm:View.WidthEx="15" Margin="1" ></TextBlock>
        </StackPanel>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

任何帮助将不胜感激。

顺便说一句,我必须在多种形式中使用这种完全相同的格式(相同的列表等,只是不同的 SelectedValue)——因此,如果您想建议在 xaml 中执行此操作的最佳方法,那就太好了.在我之前的 xaml 时代,我只会创建一个控件,设置一个属性或 2,然后在我的所有表单中使用它。但我应该使用 xaml,所以不确定最好的方法。

谢谢!

【问题讨论】:

  • 使用 SelectedItem 代替 SelectedValue
  • 效果更好,但现在我看到的是 Listed 对象的名称,而不是值。看来它正在返回对象,而不是 SelectedValuePath 中的值。
  • 您可以在代码中从该对象中提取该值
  • 但是用户不会理解显示部分的对象名称代表数据。我只想显示“代码”值,而不是类的名称。
  • IOW,我希望它像第一个示例一样工作,只显示 2 列而不是 1 列。我不敢相信我必须使用代码隐藏来获得该 UI。

标签: wpf xaml combobox


【解决方案1】:

我不敢相信我在网上搜索了一个答案,直到现在都找不到答案。答案和我想的一样简单。

只需替换: DisplayMemberPath="代码" 和 TextSearch.TextPath="代码" 并且代码工作得很好。

感谢所有帮助过的人。

【讨论】:

    【解决方案2】:

    我可以告诉你如何使用 SelectedItem

    视图模型

    public class ViewModel
        {
            public ViewModel()
            {
                //Suppose your collection CitTypes is Initialized and filled with there Items
                //Now you can set first Element as selected in ComboBox 
                SelectedItem = CitTypes.FirstOrDefault();
            }
    
            CitType selectedItem;
            public CitType SelectedItem
            {
                get { return selectedItem; }
                set { selectedItem = value; RaisePropertyChanged("SelectedItem"); }
            }
        }
    

    xaml

    <ComboBox Name="cboCit_type"
    IsSynchronizedWithCurrentItem="True"    
    mvvm:View.FlowsWithPrevious="True"
    ItemsSource="{Binding Path=cuCodeInfo.CitationTypes}"
    Text="{Binding cit_type}"
    IsEditable="true"
    IsReadOnly="false"
    **SelectedItem="{Binding SelectedItem}"**>
    

    【讨论】:

    • 是的,我正在考虑做这样的事情。或者只是创建一个结合了这两个值的属性并使用第一种方法。但我不明白。为什么 MS 让两列数据比一列复杂得多????第一个例子(一列)工作得很好......
    • 你试试这个你会习惯的。我认为SelectedItem比SelectedValue更简单。您需要注意的是 SelectedItem 必须是列表中的元素之一,我的意思是引用必须相同。
    • 是的,但我有大量由组合框 UI 确定的属性。所以现在我必须为每个可以保存 SelectedItem 返回的实例的属性创建第二个属性,并让 setter 更新真实属性。是的,它会起作用,但为什么 MS 不允许我继续使用 DisplayMemberPath 来处理多列组合框????
    • 好的,找到答案了。当他们让我时会发布。但是只需使用 TextSearch.TextPath 就可以解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2013-02-23
    • 1970-01-01
    • 2013-08-09
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多