C#C
C#3y ago
Duck

❔ How To Use Saved String Data As ButtonLabel Content

Hi

I'm fairly new to C#/XAML, however not necessarily new to coding in general (using Visual Studio).

I'm building a game launcher of some sort, and I'm trying to display a button content with saved string data, so that its displayed to the user each time the window is displayed.

I get my string data from my SQL database


            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;
                }
            }

And I want to use the data inside char1/char2/char3 as button content

However in doing this, I have failed multiple times

My code starting off, basically what I expected the end result to be:

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;
        }
    }

}


where UserIDLoggedIn = Label and CharSlot1,2,3 = Buttons, and UGCLauncher.userId, UGCLauncher.char1... = strings with stored values inside

I've tried multiple attempts at binding but none seem to generate the end result I need.

Here is the XAML code:
        <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>


Attached is picture of what happens in the application (char1, char2, and char3 data isnt displayed as content)

im fairly new and i feel like its something really obvious im missing ;;
image.png
Was this page helpful?