使用保存的字符串数据作为按钮/标签内容的C#/XAML

gpnt7bae  于 2022-12-25  发布在  C#
关注(0)|答案(1)|浏览(159)

我对C#/XAML相当陌生,但对一般的编码(使用Visual Studio)并不一定陌生。
我正在构建某种类型的游戏启动器,并尝试使用保存的字符串数据显示按钮内容,以便在每次显示窗口时向用户显示按钮内容。
从SQL数据库获取字符串数据

sql = "SELECT CNAME FROM TABLE WHERE MEMBERID = '" + user + "'";
            command = new SqlCommand(sql, cnn);
            dataReader = command.ExecuteReader();
            while (dataReader.Read())
            {
                Output = (string)dataReader.GetValue(0);
                if(char1 == "")
                {
                    char1 = Output;
                }
                else if (char2 == "")
                {
                    char2 = Output;
                }
                else if (char3 == "")
                {
                    char3 = Output;
                }
            }

我想使用char 1/char 2/char 3中的数据作为按钮内容
但是在这件事上,我失败了很多次
我的代码开始了,基本上是我所期望的最终结果:

namespace UGCAppp
{
    /// <summary>
    /// Interaction logic for SelectChar.xaml
    /// </summary>
    public partial class SelectChar : Window
    {
        public SelectChar()
        {
            InitializeComponent();
            UserIDLoggedIn.Content = "Logged in as: " + UGCLauncher.userId;
            this.DataContext = this;
            CharSlot1.Content = UGCLauncher.char1;
            CharSlot2.Content = UGCLauncher.char2;
            CharSlot3.Content = UGCLauncher.char3;
        }
    }

}

其中,用户ID登录=标签,CharSlot 1、2、3 =按钮,UGCLuncher.用户ID、UGCLuncher. char 1...=内部存储有值的字符串
我已经尝试了多次绑定,但似乎都没有产生我需要的最终结果。
我卡住了请帮忙
编辑:根据Andrew的要求,我遇到的问题是按钮确实显示了,但是字符变量中的字符串数据没有显示为按钮上的按钮内容。
下面是XAML代码:

Window x:Class="UGCAppp.SelectChar"
        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:UGCAppp"
        mc:Ignorable="d"
        Title="SelectChar" Height="450" Width="800">
    <Window.Background>
        <ImageBrush ImageSource="/launcherbg.png" Stretch="UniformToFill"/>
    </Window.Background>
    <Grid>
        <Button x:Name="BACK" Content="BACK" HorizontalAlignment="Left" Margin="27,28,0,0" VerticalAlignment="Top" FontWeight="Bold" Height="35" Width="56" Click="BACK_Click">
            <Button.Background>
                <ImageBrush ImageSource="/button7.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="MAIN" Content="MAIN" HorizontalAlignment="Left" Margin="27,85,0,0" VerticalAlignment="Top" FontWeight="Bold" Height="35" Width="56" Click="MAIN_Click">
            <Button.Background>
                <ImageBrush ImageSource="/button7.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="CharSlot1" Content="{Binding cha1, Mode=OneWay}" HorizontalAlignment="Center" Margin="0,102,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
            <Button.Background>
                <ImageBrush ImageSource="/button8.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="CharSlot2" Content="" HorizontalAlignment="Center" Margin="0,217,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
            <Button.Background>
                <ImageBrush ImageSource="/button9.png"/>
            </Button.Background>
        </Button>
        <Button x:Name="CharSlot3" Content="" HorizontalAlignment="Center" Margin="0,330,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
            <Button.Background>
                <ImageBrush ImageSource="/button10.png"/>
            </Button.Background>
        </Button>
        <Label x:Name="UserIDLoggedIn" HorizontalAlignment="Left" Margin="539,359,0,0" VerticalAlignment="Top" Foreground="White" BorderBrush="Black" Height="48" Width="234"/>
    </Grid>
</Window>

很抱歉;

lkaoscv7

lkaoscv71#

这个简单的例子展示了如何在Window中传递和显示值。

*Char1直接设置为Button
*Char2正在使用与Button的绑定。
主窗口.xaml

<Window
    x:Class="WpfApp1.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:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Button
            Click="Button_Click"
            Content="Show SelectChar window" />
    </Grid>
</Window>

主窗口.xaml.cs

using System.Windows;

namespace WpfApp1;

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SelectChar selectCharWindow = new(char1: "Char1 Button", char2: "Char2 Button");
        selectCharWindow.Show();
    }
}

选择字符xaml

<Window
    x:Class="WpfApp1.SelectChar"
    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:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="SelectCharWindow"
    Title="SelectChar"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <StackPanel>
        <Button x:Name="Char1Button" />
        <Button
            x:Name="Char2Button"
            Content="{Binding ElementName=SelectCharWindow, Path=Char2}" />
    </StackPanel>
</Window>

选择字符xaml.cs

using System.Windows;

namespace WpfApp1;

public partial class SelectChar : Window
{
    public SelectChar()
    {
        InitializeComponent();
    }

    public SelectChar(string char1, string char2)
    {
        Char2 = char2;
        InitializeComponent();
        this.Char1Button.Content = char1;
    }

    public string Char2 { get; set; } = string.Empty;
}

相关问题