如何动态更改xaml文本框?

时间:2015-07-29 19:57:00

标签: c# wpf xaml

我为该计划的状态制作了一个枚举。 当它设置为JustInstalled时,我试图使用C#更改XAML标签的内容。

CS代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApplication3
{
    enum Status
    {
        JustInstalled, 
        Configured
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class Program
    {
        public static void Main()
        {
            Status status = new Status();
            if (!File.Exists("logindata.xml"))
            {
                status = Status.JustInstalled;
                if (status == Status.JustInstalled)
                {
                   Application.Current.Resources["loginlabel"].Content = "Please enter your desired username and password.";
                }
            }
        }
    }
}

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        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:WpfApplication3"
        mc:Ignorable="d"
        Title="DFIHUDF" Height="350" Width="525"

        >
    <Grid>
        <Label FontSize="20" x:Name="label" Content="SFDGFDS" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="180,10,0,0" Width="209"/>
        <Label x:Name="loginlabel" Content="x" HorizontalAlignment="Left" Margin="84,95,0,0" VerticalAlignment="Top" Width="89"/>

    </Grid>
</Window>

2个错误是:

Error   CS1061  'object' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) WpfApplication3 C:\Users\Nathan\documents\visual studio 2015\Projects\WpfApplication3\WpfApplication3\MainWindow.xaml.cs    44

Error   CS0017  Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.    WpfApplication3 C:\Users\Nathan\documents\visual studio 2015\Projects\WpfApplication3\WpfApplication3\obj\Debug\App.g.i.cs  63

我不知道错误在说什么,因为我是WPF的新手,我的主要问题是我不知道如何使用c#动态更改标签的内容。

3 个答案:

答案 0 :(得分:4)

正如我所见,你不需要在这里引入额外的东西,如枚举,以下内容将会得到满足:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            if (!File.Exists("logindata.xml"))        
                StatusText = "Just Installed";  
            else
                StatusText = "Configured"; 


            DataContext = this; 
        }

    public string StatusText {get;set;}
}

然后你的标签:

<Label x:Name="loginlabel" Content="{Binding StatusText}" />

答案 1 :(得分:1)

您需要有一种机制来在StatusText属性更改时更新文本。这可以通过使用依赖属性和绑定来完成。

首先,将此绑定放在XAML中:

<Label x:Name="loginlabel" Content="{Binding StatusText}" />

接下来,在代码中添加依赖项属性,以便XAML知道更改:

public static readonly DependencyProperty StatusTextProperty = 
DependencyProperty.Register("StatusText", typeof(string), typeof(Window1));

public string StatusText
{
    get
    {
        return this.GetValue(StatusTextProperty) as string;
    }
    set
    {
        this.SetValue(StatusTextProperty, value);
    }
}

最后,添加一个方法以允许动态设置新状态:

public void UpdateStatus()
{
    if (!File.Exists("logindata.xml"))
    {
        StatusText = "Please enter your desired username and password.";
    }
    else
    {
        StatusText = "Logged In";
    }
}

答案 2 :(得分:0)

试试这个:

public class Program
    {
        public static void Main()
        {
            WpfApplication3.MainWindow win = new WpfApplication3.MainWindow();
            Status status = new Status();
            if (!File.Exists("logindata.xml"))
            {
                status = Status.JustInstalled;
                if (status == Status.JustInstalled)
                {
                   win.loginlabel.Content = "Please enter your desired username and password.";

                }

                Application.Run(win);
            }
        }
    }