问题设置的SelectedValue的组合框在XAML [英] Problem setting selectedvalue for combobox in xaml

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

问题描述

下面是我的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 ={结合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>

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

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