如何启用文本框以包含默认值并能够在WP8中编辑它?

时间:2014-03-27 23:37:07

标签: c# xaml windows-phone-8

我正在扫描文档并保存三个值。

我想创建一个在扫描后出现的弹出窗口,它将包含3个文本框。默认情况下,每个文本框都会包含扫描值,当我按下文本框进行编辑时,我将无法进行编辑。

我查看了文本框属性,但只找到"只读"属性。 除此之外,我真的不知道如何开始和寻找什么。

同时我有这个xaml描述弹出窗口:

<Popup x:Name="AfterScan" Grid.Row="2">
    <Border  BorderThickness="2" Margin="10" BorderBrush="{StaticResource PhoneAccentBrush}">
        <StackPanel Background="Gainsboro" Width="434" Height="400">
            <TextBlock Text="Bill Details" TextAlignment="Center" FontSize="40" Margin="10,0" Foreground="Black"/>
            <TextBlock Text="Please review Bill Details and click OK when done" FontSize="17" Margin="40,0,10,0" Foreground="Black"/>
            <StackPanel  Orientation="Horizontal" Margin="0,10">
                <TextBox x:Name="test"  Width="100" Margin="10,0,10,0" Foreground="Black" BorderBrush="Black"/>
            </StackPanel>
            <StackPanel  Orientation="Horizontal" Margin="0,10">
                <TextBox x:Name="test2"  Width="100" Margin="10,0,10,0" Foreground="Black" BorderBrush="Black"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,10">
                <Button x:Name="btn_OK"  Content="OK" Width="215" Margin="110,0,10,0" Click="EnsuredDetails_Click" Foreground="Black" BorderBrush="Black"/> 
            </StackPanel>

        </StackPanel>
    </Border>
</Popup>

我知道如果我写了一个值 - 我可以在以后使用它,但我希望文本框包含我的扫描值。

我也看过这篇文章: http://mrbool.com/how-to-build-a-custom-textbox-in-windows-phone/25846

我的问题是这个文本框的默认值是在xaml代码中声明的字符串,我需要它作为动态参数:

 <my:DefaultTxt_box Height="103" HorizontalAlignment="Left" Margin="-4,49,0,0"Name="defaultTxt_box1"
                           Text="" D_Text="Enter Your Name" VerticalAlignment="Top" Width="460"FontSize="32" FontFamily="Comic Sans MS" Foreground="#FFFFFF4C">
            <my:DefaultTxt_box.Background>

有人可以指导我一点吗?

非常感谢!

4 个答案:

答案 0 :(得分:1)

你必须选择完成这项工作。

选项1)

您可以将textbox的text属性与动态值属性绑定。为此,您可以使用this链接。

选项2)

你会在xaml的弹出窗口中有三个文本框。

<TextBox x:Name="test"  Width="100" Margin="10,0,10,0" Foreground="Black" BorderBrush="Black" Text="your default value will go here"/>

<TextBox x:Name="test_2"  Width="100" Margin="10,0,10,0" Foreground="Black" BorderBrush="Black" Text="your default value will go here"/>

<TextBox x:Name="test_3"  Width="100" Margin="10,0,10,0" Foreground="Black" BorderBrush="Black" Text="your default value will go here"/>

然后,您可以从c#代码后面的代码设置文本框的文本属性。为此,首先您必须从扫描的文档中获取价值。

假设您在变量ScannedValue1,ScannedValue2和ScannedValue3

中获得了这些值
String ScannedValue1 ; //You will have to assigned values from scanned copy here
String ScannedValue2 ; //You will have to assigned values from scanned copy here
String ScannedValue3 ; //You will have to assigned values from scanned copy here

然后,您可以利用这些值将它们分配给弹出的文本框。

test.Text = ScannedValue1;
test_2.Text = ScannedValue2;
test_3.Text = ScannedValue3; 

希望你能从中得到这个想法:)

答案 1 :(得分:0)

这可能是您正在寻找的:

Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";

myTxtbx.OnFocus += OnFocus.EventHandle(RemoveText);
myTxtbx.LoseFocus += LoseFocus.EventHandle(AddText);

public RemoveText(object sender, EventArgs e)
{
     myTxtbx.Text = "";
}

public AddText(object sender, EventArgs e)
{
     if(myTxtbx.Text == "")
        myTxtbx.Text = "Enter text here...";
}

Adding placeholder text to textbox

答案 2 :(得分:0)

每个textBox都应该有一个x:Name属性,只需在你的C#代码中设置它:

myTextBox.Text = 'myScannedValue';

'myTextBox'是你的x:TextBoxin XAML的名字。试试吧。

答案 3 :(得分:0)