根据数据源放入文本框中的值更新绑定到数据源的文本框的内容

发布于 2024-11-04 13:42:59 字数 714 浏览 1 评论 0原文

我有一个文本框,它绑定到数据服务以获取其内容。目前,数据服务将 1 到 9 之间的数字放入该文本框中。我需要做的是根据该值用字符串替换该文本框的内容。因此,例如,如果文本框的原始内容是“1”,则它将被替换为“1 - 此处的示例文本”

下面是定义文本框的代码。

<StackPanel Margin="0,0,0,17" Width="432">
    <TextBlock Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>

我认为我可能会使用 else if 语句,但我不知道如何从该 if 语句中引用文本块。

感谢您的帮助

I have a text box which is bound to a data service to get its contents. At the moment the data service puts a number between 1 and 9 into that text box. What I need to do is based on that value replace the contents of that text box with a string. So for example if the original contents of the textbox was "1" instead it would be replaced with "1 - Example text here"

Below is the code where the text box is defined.

<StackPanel Margin="0,0,0,17" Width="432">
    <TextBlock Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>

I thought that possibly I would use an else if statement but I don't know how to reference the textblock from within that if statement.

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-11-11 13:42:59

您需要命名 TextBlock,以便后面的代码可以引用它,这样下面的代码就可以工作

 <TextBlock x:Name="tb1" Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>

if (tb1.Text == "something")
        {
            DoSomething();
        }
        else
        {
            DoSomethingElse();
        }

You need to name the TextBlock so it can ve referenced by the code behind so something like the code below will work

 <TextBlock x:Name="tb1" Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>

if (tb1.Text == "something")
        {
            DoSomething();
        }
        else
        {
            DoSomethingElse();
        }
二智少女猫性小仙女 2024-11-11 13:42:59

您可以定义一个值转换器。例如:

public class IntToTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Do the conversion from int to Text
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Do the conversion from Text to int
    }
}

<Window x:Class="MyNamespace.Window1"
    ...
    xmlns:my="clr-namespace:MyNamespace"
    ...>
    <Window.Resources>
        <my:IntToTextConverter x:Key="converter" />
    </Window.Resources>
    <Grid>
        <TextBox Text={Binding Category1, Converter={StaticResource converter}}/>
    </Grid>
</Window>

这里有一篇关于值转换器的好文章

You can define a value converter. For example:

public class IntToTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Do the conversion from int to Text
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Do the conversion from Text to int
    }
}

<Window x:Class="MyNamespace.Window1"
    ...
    xmlns:my="clr-namespace:MyNamespace"
    ...>
    <Window.Resources>
        <my:IntToTextConverter x:Key="converter" />
    </Window.Resources>
    <Grid>
        <TextBox Text={Binding Category1, Converter={StaticResource converter}}/>
    </Grid>
</Window>

There is a good article about value converters here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文