0

I have a textbox

<TextBox Height="23" Grid.Column="1" PreviewTextInput="AddressBox_PreviewTextInput" HorizontalAlignment="Right" Margin="0,27,13,0" Name="AddressI2C" Text="{Binding Path=AddressMessage, Mode=TwoWay}" VerticalAlignment="Top" Width="128" />

    private string _AddressMessage = string.Empty;
    public string AddressMessage
    {
        get
        {
            return _AddressMessage;
        }
        set
        {
            _AddressMessage = value;
            NotifyPropertyChanged("AddressMessage");
        }
    }

Now In my view Model class I have a method which requires me to get the text stored in this textbox and save it in a int variable. Now here is the trick, I have to save only the hexadecimal values inside this variable.

Demonstration:

Textbox value: 0x18

So I should first of all take the text inside the textbox and store only the hexadecimal value inside the variable int. Basically store only 18 inside the integer variable.

I had done this in my C++ aplication as follows:

int address = m_texteditAddress->getText().getHexValue32();

I tried doing the following:

string strValue = AddressMessage;
if(strValue.StartsWith("0x"))
{
    strValue = strValue.Remove(0,2);
    int address = Convert.ToInt32(strValue);         
}

but AddressMessage is always empty even though I am entering "0x23" when I debug the code. The control doesnt enter the loop. Now how can I achieve this????

4

2 回答 2

0

尝试这个:

int address = int.Parse(MyTextBox.Text, System.Globalization.NumberStyles.HexNumber);
于 2012-10-04T04:54:00.827 回答
0

由于该值绑定到该AddressMessage属性,因此只需获取该值即可。此外,您的示例只是从十六进制字符串中删除了“0x”,所以只需使用String.SubString().

例子:

int address = int.Parse(AddressMessage.Substring(2));

如果您需要将十六进制字符串实际转换为它代表的实际整数,请使用System.Convert

int address = Convert.ToInt32(AddressMessage, 16);
于 2012-10-04T05:41:54.523 回答