You must Sign In to post a response.
  • Category: WPF

    Button content needs to change at runtime

    Hi Friends,

    I worked in WPF application with the user control and resource files.

    Here my need is to change the content of the button in main page at runtime.
    The values are getting properly from resource files related to the values as i passed,but that values are not updated in button at runtime after the process completes.

    Here are i will attached my application source as follows,


    Usercontrol page

    winonlineusercontrol.xaml:
    <UserControl x:Class="winonlinepetrochemical.winonlineusercontrol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="600">
    <Grid Background="GhostWhite">

    <Grid.RowDefinitions>

    <RowDefinition Height="492"></RowDefinition>
    <RowDefinition Height="8*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="635"></ColumnDefinition>
    <ColumnDefinition Width="43*" />
    </Grid.ColumnDefinitions>

    <TextBlock HorizontalAlignment="Center" Text="Welcome To WinOnline Petroleum" Name="txtheadermsg" FontWeight="Bold" FontFamily="Arial" FontSize="14" Margin="10,15,375,412" Width="250"></TextBlock>

    <StackPanel Height="68" HorizontalAlignment="Left" Margin="266,12,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="330">
    <Button Height="61" Name="btnausenglish" Margin="-230,5,15,-60" Width="76" Click="btnausenglish_Click">
    <Image Source="/images/300px-Flag_of_Australia.svg.png" Height="40" Width="70"></Image>
    </Button>
    <Button Height="61" Name="btnfrench" Margin="-55,0,25,60" Width="76" Click="btnfrench_Click">
    <Image Source="/images/Flag_of_France.svg.png" Height="40" Width="70"></Image>
    </Button>
    <Button Height="61" Name="btnitaly" Margin="110,-125,25,60" Width="76" Click="btnitaly_Click">
    <Image Source="/images/Flag_of_Italy.svg.png" Height="40" Width="70"></Image>
    </Button>
    <Button Height="61" Name="btngerman" Margin="250,-125,2,60" Width="76" Click="btngerman_Click">
    <Image Source="/images/250px-Flag_of_Germany.svg.png" Height="40" Width="70"></Image>
    </Button>
    </StackPanel>
    <TextBlock HorizontalAlignment="Center" Text="" Name="txthiddendisplay" Width="100" Height="30" Visibility="Hidden"></TextBlock>

    </Grid>
    </UserControl>

    winonlineusercontrol.xaml.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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.Resources;
    using LangTrans;

    namespace winonlinepetrochemical
    {
    /// <summary>
    /// Interaction logic for winonlineusercontrol.xaml
    /// </summary>
    public partial class winonlineusercontrol : UserControl
    {
    public string language, message;
    string strLanguage = "";


    public winonlineusercontrol()
    {
    InitializeComponent();
    }
    private void btnausenglish_Click(object sender, RoutedEventArgs e)
    {
    languageselected("English");
    Mainpage mp = new Mainpage();
    mp.translatelanguage(App.Current.Properties["languagechosed"].ToString());
    }

    private void btnfrench_Click(object sender, RoutedEventArgs e)
    {
    languageselected("French");
    Mainpage mp = new Mainpage();
    mp.translatelanguage(App.Current.Properties["languagechosed"].ToString());
    }

    private void btnitaly_Click(object sender, RoutedEventArgs e)
    {
    languageselected("Italy");
    Mainpage mp = new Mainpage();
    mp.translatelanguage(App.Current.Properties["languagechosed"].ToString());
    }

    private void btngerman_Click(object sender, RoutedEventArgs e)
    {
    languageselected("German");
    Mainpage mp = new Mainpage();
    mp.translatelanguage(App.Current.Properties["languagechosed"].ToString());
    }

    public void languageselected(string TerminalLanguage)
    {

    App.Current.Properties["languagechosed"] = TerminalLanguage;
    txthiddendisplay.Text = TerminalLanguage;

    if (TerminalLanguage == "")
    {
    TerminalLanguage = "English";
    }
    strLanguage = "winonlinepetrochemical.Languages." + TerminalLanguage.ToString();
    ResourceManager LocRM = new ResourceManager(strLanguage, typeof(winonlineusercontrol).Assembly);

    txtheadermsg.Text = LocRM.GetString("lblheadermsg");

    }


    }
    }

    mainpage;

    mainpage.xaml.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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.Shapes;
    using System.Resources;
    using LangTrans;

    namespace winonlinepetrochemical
    {
    /// <summary>
    /// Interaction logic for Mainpage.xaml
    /// </summary>
    public partial class Mainpage : Window
    {
    public string language;

    public string printlng,tanklng;

    public Mainpage()
    {
    InitializeComponent();
    }
    public bool IsPostBack { get; set; }




    private void btnprint_Click(object sender, RoutedEventArgs e)
    {



    }

    private void btntank_Click(object sender, RoutedEventArgs e)
    {
    Tankselection tnk = new Tankselection();
    tnk.Show();
    }



    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    //if (!IsPostBack)
    //{
    winonlineusercontrol wn = new winonlineusercontrol();
    language = wn.txthiddendisplay.Text;
    wn.languageselected(language);
    translatelanguage(language);



    //Translator t = new Translator();
    //btnprint.Content = t.Translate(this.btnprint.Content.ToString(), "English", App.Current.Properties["languagechosed"].ToString());
    //btntank.Content = t.Translate(this.btntank.Content.ToString(), "English", App.Current.Properties["languagechosed"].ToString());
    //}

    }

    public void translatelanguage(string language)
    {
    if (language == "")
    {
    language = "English";
    }
    ResourceManager LocRM = new ResourceManager("winonlinepetrochemical.Languages." + language.ToString(), typeof(Mainpage).Assembly);
    printlng = LocRM.GetString("print").ToString();
    tanklng=LocRM.GetString("Tank").ToString();
    btnprint.Content =printlng ;
    btntank.Content = tanklng ;


    //stackPanel2.UpdateLayout();

    }

    private void winonlineusercontrol2_Loaded(object sender, RoutedEventArgs e)
    {

    }
    }
    }

    mainpage.xaml:
    <Window x:Class="winonlinepetrochemical.Mainpage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Home Page" Height="600" Width="640" xmlns:my="clr-namespace:winonlinepetrochemical" Loaded="Window_Loaded">
    <Grid Background="GhostWhite">

    <Grid.RowDefinitions>

    <RowDefinition Height="492"></RowDefinition>
    <RowDefinition Height="8*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="635"></ColumnDefinition>
    <ColumnDefinition Width="43*" />
    </Grid.ColumnDefinitions>


    <my:winonlineusercontrol HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="winonlineusercontrol2" VerticalAlignment="Top" Height="449" Width="606" Loaded="winonlineusercontrol2_Loaded" />
    <StackPanel Height="157" HorizontalAlignment="Left" Margin="138,153,0,0" Name="stackPanel2" VerticalAlignment="Top" Width="355" >
    <Button Content="PRINT" Height="100" Name="btnprint" Width="160" Margin="-160,30,0,0" Click="btnprint_Click" />
    <Button Content="TANK" Height="100" Name="btntank" Width="160" Margin="180,-100,0,0" Click="btntank_Click" />
    </StackPanel>
    </Grid>
    </Window>




    here my need is to change the content of button in main page at runtime.

    Please anyone give a solution for this,its should be helpful for me to complete.
  • #761967
    Hi

    I think IsPostBack property is not allowing the page submission after you click the button related to specific language.

    Use break point for button click logic and see what actually the cause is.

    Regards

    Sridhar Thota.
    If you learn't from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #762028
    no am getting the proper values in code behind perfect but the problem is the retrieved value of button content is not update and displayed in UI side of windows application using wpf


Sign In to post your comments