从C#更改WPF TextBlock中的文本时出现问题 [英] Issue with changing text in a WPF TextBlock from C#

查看:79
本文介绍了从C#更改WPF TextBlock中的文本时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello all


我正在尝试更改textBlock中的文本,但我无法...我使用的代码是:


< pre class ="prettyprint">< TextBlock Name =" WelcomeTitle"
Grid.Column =" 1"
Horizo​​ntalAlignment =" Left"
高度="35"
Margin =" 0,300,0,0"
Grid.Row =" 1"
TextWrapping =" Wrap"
VerticalAlignment =" Top"
Width =" 626"
Text =" WELCOME!"
Foreground =" Black" />

我尝试使用我在互联网上找到的不同代码。我确实看到了"欢迎!"文本如XAML中所示。我想在代码中更改它的原因是标题和消息(在上面的代码中没有显示),它将根据用户选择的语言的
而变化。这个新手错过了吗?


非常感谢



Rick

解决方案

嗨RickHille,


>>这个新手错过了什么?


因为代码没有完成,我无法在我这边重现这个问题,这是一个示例,它通过编程方式更改Textblock文本。


#XAML

< Window x:Class =" ColeWPFSample.TextBlockSample.Window1" 
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local =" clr-namespace:ColeWPFSample.TextBlockSample"
mc:Ignorable =" d"
Title =" Window1"高度= QUOT; 450"宽度= QUOT; 800"装载= QUOT; Window_Loaded">
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =" auto">< / RowDefinition>
< RowDefinition Height =" auto">< / RowDefinition>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =" auto">< / ColumnDefinition>
< ColumnDefinition Width =" auto">< / ColumnDefinition>
< /Grid.ColumnDefinitions>
< TextBlock Name =" WelcomeTitle"
Grid.Column =" 1"
Horizo​​ntalAlignment =" Left"
高度="35"
Margin =" 0,300,0,0"
Grid.Row =" 1"
TextWrapping =" Wrap"
VerticalAlignment =" Top"
Width =" 626"
Text =" WELCOME!"
Foreground =" Black" />
< / Grid>
< / Window>

#Code Behind

使用System.Windows; 

namespace ColeWPFSample.TextBlockSample
{
///< summary>
/// Window1.xaml的互动逻辑
///< / summary>
public partial class Window1:Window
{
private string language;
public Window1()
{
InitializeComponent();

language =" zh-CN" ;;

}

private void Window_Loaded(object sender,RoutedEventArgs e)
{
string welcomeTitleValue =" WELCOME!" ;;
开关(语言)
{
case" zh-CN":
welcomeTitleValue ="欢迎!" ;;
休息;
case" en-US":
welcomeTitleValue =" WELCOME!" ;;
休息;
}
WelcomeTitle.Text = welcomeTitleValue;
}
}
}



祝你好运,


张龙


Hello all

I am trying to change text within a textBlock but am unable to... The code I used is:

<TextBlock Name="WelcomeTitle" 
Grid.Column="1" 
HorizontalAlignment="Left" 
Height="35" 
Margin="0,300,0,0" 
Grid.Row="1" 
TextWrapping="Wrap" 
VerticalAlignment="Top" 
Width="626" 
Text="WELCOME!" 
Foreground="Black"/>

I tried with different code that I found on the internet. I do see the "Welcome!" Text as shown in the XAML. The reason I want to change it in code is that the title and message (that is not shown in the above code), is that it will vary depending on the language selected by the user. Is there something this novice missed?

Many thanks

Rick

解决方案

Hi RickHille,

>>Is there something this novice missed?

Because the code is not completed, I could not reproduce the issue on my side, here is a sample, which changing the Textblock text through programmatically.

#XAML

<Window x:Class="ColeWPFSample.TextBlockSample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ColeWPFSample.TextBlockSample"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Name="WelcomeTitle" 
        Grid.Column="1" 
        HorizontalAlignment="Left" 
        Height="35" 
        Margin="0,300,0,0" 
        Grid.Row="1" 
        TextWrapping="Wrap" 
        VerticalAlignment="Top" 
        Width="626" 
        Text="WELCOME!" 
        Foreground="Black"/>    
    </Grid>
</Window>

#Code Behind

using System.Windows;

namespace ColeWPFSample.TextBlockSample
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private string language;
        public Window1()
        {
            InitializeComponent();

            language = "zh-CN";
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string welcomeTitleValue = "WELCOME!";
            switch (language)
            {
                case "zh-CN":
                    welcomeTitleValue = "欢迎!";
                    break;
                case "en-US":
                    welcomeTitleValue = "WELCOME!";
                    break;
            }
            WelcomeTitle.Text = welcomeTitleValue;
        }
    }
}

Best regards,

Zhanglong


这篇关于从C#更改WPF TextBlock中的文本时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆