在 xaml 中为组合框设置 selectedvalue 时出现问题 [英] Problem setting selectedvalue for combobox in xaml

查看:19
本文介绍了在 xaml 中为组合框设置 selectedvalue 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 xaml 片段:

Here is a snippet of my xaml:

    <ComboBox x:Name="cbo1" Width="100" SelectedValue="200">
        <ComboBoxItem Name="n1">100</ComboBoxItem>
        <ComboBoxItem Name="n2">200</ComboBoxItem>
    </ComboBox>

为什么这不起作用?运行它时未选择200".理想情况下,我正在尝试执行 SelectedValue="{Binding MyValue}".

Why doesn't this work ? The '200' is not selected when I run it. Ideally, I am trying to do SelectedValue="{Binding MyValue}".

推荐答案

本例中选择的值来自 ComboBoxItem 类型,而不是您希望的整数或字符串.

The selected value in this case is from type ComboBoxItem and not integer or string as you wished.

那你能做些什么呢?组合框存在一个属性,它定义了所选对象的哪个属性应该用作值,哪个属性用作 DisplayMember(可视化)

so what can you do against that? there exists a property for the combobox which defines which property of the selected object should be used as value and which as DisplayMember (the visualization)

在您的情况下,您必须将 SelectedValuePath 设置为内容".(在您的情况下,200 是 ComboBoxItem 的内容)

in your case you have to set the SelectedValuePath to "Content". (The 200 are in your case the content of the ComboBoxItem)

示例:

<ComboBox x:Name="cbo1" Width="100" SelectedValue="200" SelectedValuePath="Content">
    <ComboBoxItem Name="n1">100</ComboBoxItem>
    <ComboBoxItem Name="n2">200</ComboBoxItem>
</ComboBox>

这篇关于在 xaml 中为组合框设置 selectedvalue 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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