我正在我的 wpf 应用程序中处理文本框和组合框。我很抱歉标题很奇怪。好吧,这是场景:
xml:
<ComboBox ItemsSource="{Binding PortModeList}" SelectedItem="{Binding SelectedPortModeList, Mode=OneWayToSource}" SelectedIndex="0" Name="PortModeCombo" />
<TextBox Grid.Column="1" Text="{Binding OversampleRateBox}" Name="HBFilterOversampleBox" />
视图模型类:
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");
        }
    }
这里我有三个要求:
- 通过 - SelectedId在 xaml 中使用,我可以选择 id,但我想从 viewmodel 类中设置我的组合框的 selectedid。即- int portorder = 2 PortModeList->SetSelectedId(portOrder)。我怎么能做这样的事情?还是他们有其他方法?
- 我需要将文本框中的条目数限制为 4。即在文本框中输入 1234,它不应让用户超过 4 位。 
- 我想将文本格式设置 - OversampleRateBox为:0x__。即,如果用户想在变量中输入 23,那么我将文本设置为 0x23。基本上 0x 应该出现在开头。
请帮忙 :)