Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good morning i am trying to change the button context via code so by default button has no text

when i try and set context for the button this is what i have so far

What I have tried:

xaml
XML
<Button x:Name="vsbutton"
        Content="{Binding Elfenlied,
                  UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
        Foreground="BlueViolet"
        Margin="4"
        Height="20"
        Command="{Binding vstest}">

InstallVM
C#
public string Elfenlied
{
    get { return _elfenlied; }
    set 
    {
        _elfenlied = value; 
        OnPropertyChanged(nameof(Elfenlied));
    }
}


C#
public InstallVM()
{
    try
    {
        Elfenlied = WeakReferenceMessenger
            .Default
            .Send<VSInstalledMessage>();
    }
    catch(Exception ex)
    {
               
    }
}

i have put in try catch

as sometimes it does retun null if download view on menu has not been selected before going into install working on fixing that

but when it changes and i debug and elfenlied has a value of vs already installed

this is not reflected on the button text it still showing up as blank
With Value
code behind

UI
ui

any help would be much appreciated
Posted
Updated 16-Mar-23 7:54am
v3

Your code has a few issues:
1. I can not see where you set the DataContext binding the ViewModel to the View.

2. Your button has two errors. Change:
XML
<Button x:Name="vsbutton"
        Content="{Binding Elfenlied,
                  UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
        Foreground="BlueViolet"
        Margin="4"
        Height="20"
        Command="{Binding vstest}">

To:
XML
<Button x:Name="vsbutton"
        Content="{Binding Elfenlied}"
        Foreground="BlueViolet"
        Margin="4"
        Height="20"
        Command="{Binding VsTestCommand}">

Where:
a. Elfenlied is a public property in your ViewModel - it appears that you have done this.
b. VsTestCommand has the ICommand inplemented in your ViewModel to process the Button Click event.

Lastly, I have no Idea what you are doing with this...
C#
public InstallVM()
{
    try
    {
        Elfenlied = WeakReferenceMessenger
            .Default
            .Send<VSInstalledMessage>();
    }
    catch(Exception ex)
    {
               
    }
}

Are you trying to send a message to another part of the app when the class is created???
 
Share this answer
 
Comments
Maciej Los 16-Mar-23 14:43pm    
5ed!
in xzaml
<UserControl.DataContext>
    <vm:InstallVM/>
</UserControl.DataContext>



and yes the code

try
{
    Elfenlied = WeakReferenceMessenger
        .Default
        .Send<VSInstalledMessage>();
}
catch(Exception ex)
{

}


so from the downloadvm it generates the message

the class it attaches to is just a holder for the messanger from the tool kit so all vsinstallmessage holds is this,

public class VSInstalledMessage : RequestMessage<string>
{

}


then when download is clicked it genreates the vsisinstalled public string in download

here is a snippet of it

public DownloadVM()
{
    cvs.Source = GetData();


    WeakReferenceMessenger.Default.Register<DownloadVM, VSInstalledMessage>(this, (r, m) =>
    {
        // Assume that "CurrentUser" is a private member in our viewmodel.
        // As before, we're accessing it through the recipient passed as
        // input to the handler, to avoid capturing "this" in the delegate.
        m.Reply(r.vsinstalledstring);
    });

    spotifydownloader.checkvm(elfendownload.IsVC2015Installed());
}



public bool Vs2015Installed_CanExecute()
{
    Test = spotifydownloader.test;
    if (spotifydownloader.test == "True")
    {
        vsinstalledstring = "Already Installed";



        return false;
    }
    else
    {

    }

    vsinstalledstring = "Install Vs";
    return true;
}


so from here it generates what it should be and then will send to installvm when clicked on the result of this and i want to add this to the button text

because when i wire it up correctly it will check if its installed and if not the button will be enabled if its already installed it then it greys it out with already installed.

and if its not its active we install it once its installed it checks again and disables it but doing 1 step at a time at the moment

if that makes better sense ?
 
Share this answer
 
Comments
[no name] 16-Mar-23 14:44pm    
You just answered your own question so for all intents the matter is closed.
Maciej Los 16-Mar-23 14:46pm    
Is this solution or continuation of conversation?
elfenliedtopfan5 16-Mar-23 21:08pm    
umm unsure to how I answered my own question when the issue is the button text is not updating that is not answering my own question because that issue still persists.
i was giving Graeme_Grant more information based on what he has suggested and the questions he asked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900