WPF:可编辑的ComboBox和StringFormat [英] WPF: Editable ComboBox and StringFormat

查看:1307
本文介绍了WPF:可编辑的ComboBox和StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(首先,我简化了示例以供您理解。)

(First of all, I simplified the sample for you to understand.)

我想要一个可编辑 ComboBox 。 原始(来源)值是以像素为单位的值(DIP),因为这是应用程序在内部工作的单位。用户不应该看到以像素为单位的值,而是以英寸(UI的测量单位)为单位。我确定我必须使用转换器将像素转换为英寸,反之亦然。

I want a editable ComboBox that shows values in inches. The original (source) value is a value in pixels (DIPs), because this is the unit in which the application works internally. The user shouldn't see the value in pixels, but in inches (the measurement unit of the UI). I determined that I must use a converter to translate from pixels to inches and viceversa.

ComboBox项目也应使用{0}格式。

The ComboBox items should also be formatted with "{0} in".

应该像这样

[ 4 in  |v]
  [ 1 in  ]
  [ 2 in  ]
  [ 3 in  ]
  [ 4 in  ]

但是,请记住,内部它应该在Pixels工作!并可编辑。这是工作的一部分。我完全无法实现这一点。

But, remember, internally it should work in Pixels! and Be Editable. This is the works part. I have been totally unable to achieve this.

到目前为止:

  <ComboBox IsEditable="True"
            ItemStringFormat="'{}{0} in}'"                  
            Text="{Binding RelativeSource={RelativeSource Self}, Path=SelectedValue, Mode=TwoWay, Converter={StaticResource MyPixelToInchesConverter}, StringFormat='{}{0} in}'}">
            <system:Double>1</system:Double>
            <system:Double>2</system:Double>
            <system:Double>3</system:Double>
  </ComboBox>

问题?当您选择一个项目时,它没有格式化。当你在ComboBox的可编辑区域中编写一个数字时,它也不会被格式化。

The problem? When you select an item, it's not formatted. And when you write a number in the editable region of the ComboBox, it's not formatted, too.

因此,对于ComboBox来说,用户输入不存在的值??

So, what is the best way for a ComboBox to format items even when the user enters a non-existent value??

谢谢!

推荐答案

p>可能有点晚了,但我发现设置 Text 属性来使用绑定可以工作,有点kludge。在我的例子中,我使用一个Currency值,但是应该应用相同的原则

Probably a bit late, but I found that setting the Text property to use a binding can work, with a bit of a kludge. In my case I was working with a Currency value, but the same principles should apply

Text = {Binding Path=Indemnity, StringFormat=C0}

但是,输入不在列表中的文本值仍然存在 StringFormat 不适用(因为某些奇怪的原因只有设计此MS的MS人知道)。

However, entering a text value not in the list still exhibits the problem that the StringFormat is not applied (for some strange reason known only to the MS folks who designed this).

,我添加了一个 LostFocus 处理程序,它只是写了一个不正确的值,然后再次写入正确的值 - StringFormat 应用:

To work around this, I added a LostFocus handler that simply writes an incorrect value, followed by writing the correct value again - at which stage the StringFormat is applied:

private void IndemnityCombo_LostFocus(object sender, RoutedEventArgs e)
{
    var vm = this.DataContext as RatesViewModel;    // A view model class
    if (vm != null) {
        decimal indemnity = vm.Indemnity;
        vm.Indemnity = indemnity + 1M;
        vm.Indemnity = indemnity;
    }
}

这不是优雅, 。希望对其他用户有用。

It's not elegant, but it does the job for me. Hope it can be useful to other users.

这篇关于WPF:可编辑的ComboBox和StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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