我对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>
很抱歉;
1条答案
按热度按时间lkaoscv71#
这个简单的例子展示了如何在
Window
中传递和显示值。*Char1直接设置为
Button
。*Char2正在使用与
Button
的绑定。主窗口.xaml
主窗口.xaml.cs
选择字符xaml
选择字符xaml.cs