【问题标题】:How to set SelectedId of Combobox and set text of Textbox in a specific format from ViewModel in WPF如何在 WPF 中的 ViewModel 中设置 Combobox 的 SelectedId 并以特定格式设置 Textbox 的文本
【发布时间】:2012-10-22 11:11:25
【问题描述】:

我正在我的 wpf 应用程序中处理文本框和组合框。我很抱歉奇怪的标题。好吧,这是场景:

Xaml:

<ComboBox ItemsSource="{Binding PortModeList}" SelectedItem="{Binding SelectedPortModeList, Mode=OneWayToSource}" SelectedIndex="0" Name="PortModeCombo" />

<TextBox Grid.Column="1" Text="{Binding OversampleRateBox}" Name="HBFilterOversampleBox" />

ViewModel 类:

public ObservableCollection<string> PortModeList
    {
        get { return _PortModeList; }
        set
        {
            _PortModeList = value;
            OnPropertyChanged("PortModeList");
        }
    }

    private string _selectedPortModeList;
    public string SelectedPortModeList
    {
        get { return _selectedPortModeList; }
        set
        {
            _selectedPortModeList = value;                
            OnPropertyChanged("SelectedPortModeList");
        }
    }

    private string _OversampleRateBox;
    public string OversampleRateBox
    {
        get
        {
            return _OversampleRateBox;
        }

        set
        {
            _OversampleRateBox = value;
            OnPropertyChanged("OversampleRateBox");
        }
    }

这里我有三个要求:

  1. 通过使用 SelectedIdin xaml 我可以选择 id,但我想从 viewmodel 类中设置我的组合框的 selectedid。 IE。 int portorder = 2 PortModeList->SetSelectedId(portOrder)。我怎么能做这样的事情?还是他们有其他方法?

  2. 我需要将文本框中的条目数限制为 4。即1234在文本框中输入,它不应让用户超过4位。

  3. 我想将OversampleRateBox中的文本格式设置为:0x__。 IE。如果用户想在变量中输入 23,那么我会将文本设置为 0x23。基本上0x应该出现在开头。

请帮忙:)

【问题讨论】:

    标签: c# .net wpf combobox textbox


    【解决方案1】:

    我会使用ComboBoxSelectedItem 属性(就像你正在做的那样),但是进行双向绑定。然后,您可以在视图模型中设置您的SelectedPortModeList(应该称为SelectedPortMode)。

    <ComboBox ItemsSource="{Binding PortModeList}" 
           SelectedItem="{Binding SelectedPortMode}" ...
    

    在视图模型中:

    // Select first port mode
    this.SelectedPortMode = this.PortModeList[0];
    

    如果您想限制TextBox 中的字符数,请使用MaxLength 属性:

    <TextBox MaxLength="4" ... />
    

    如果您希望为OversampleRateBox 添加前缀,一种选择是在OversampleRateBox 的设置器中测试该前缀是否存在,如果不存在,则在分配给您的私有字段之前添加它.

    更新

    将您的TextBox 绑定到字符串属性:

    <TextBox Grid.Column="1" Text="{Binding OversampleRateBox}" ... />
    

    在您的属性的设置器中,在设置您的私有字段之前检查输入是否有效:

    public string OversampleRateBox
    {
        get
        {
            return _OversampleRateBox;
        }
    
        set
        {
            // You could use more sophisticated validation here, for example a regular expression
            if (value.StartsWith("0x"))
            {
                _OversampleRateBox = value;
            }
    
            // Now if the value entered is not valid, then the text box will be refreshed with the old (valid) value
            OnPropertyChanged("OversampleRateBox");
        }
    }
    

    因为绑定是双向的,你也可以从你的视图模型代码中设置值(例如在视图模型构造函数中):

    this.OversampleRateBox = "0x1F";
    

    【讨论】:

    • 感谢您的回复。要求 1 和 2 很明确,但我仍然无法弄清楚如何继续进行第 3 项。你能用一个示例代码详细说明吗:)
    • 我不确定您的意思,因为您没有在视图或视图模型的任何位置定义“SelectedId”。此外,您在 SelectedPortMode 属性中有选定项,因此我会将其从字符串更改为表示对象的实际类型。例如,这可能只是一个具有 Value 和 Text 属性的 ListItem 类型。
    • 你是对的。我明白了:) 现在将OverSampleRateBox 的前缀设置为0x 呢???
    • 正如我所说,您可以在 setter 中进行操作。但是,您是否打算在用户键入时即时更改他们的输入?这可能是一种很奇怪的经历。您可以在显示“0x”的文本框之前有一个标签,然后在您需要他们输入的值时,只需将他们的输入与 0x 前缀结合起来。
    • 好吧,我在我的 C++ 应用程序中这样做了:tmp = m_filterRegs[i][4] &amp; 0x1F; // 假设 tmp 值为 12。然后我将字符串设置为:m_cicInterpolRate-&gt;setText(String(T("0x")) +String::toHexString((int)tmp));。这基本上需要0x12。如何才能做到这一点? :) 如果您能用代码解释一下,将不胜感激 :)
    猜你喜欢
    • 2020-10-14
    • 2014-11-11
    • 2018-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多