1

The person is choosing smth from combobox and I have to save this value and transport it to another page ?

public string ToAnotherWin() {
    k = comboboxPrices.SelectedItem.ToString();
    return k;
}
Tsahi Asher
  • 1,767
  • 15
  • 28

1 Answers1

1

First create a class that stores your variable (myVar):

public class myData
{
    public string myVar { get; set; }
} 

Then create a static class with a variable of type myData :

public static class allData
{
    public static myData myData {get;set;}
}

This allows you to call the variable myVar in another window.

If your combobox is positioned in the MainWindow your MainWindow constructor should look like this:

public MainWindow()
{
     InitializeComponent();
     comboBox.ItemsSource = new string[] { "aaa","smth","bbb" };
     allData.myData = new myData();
     DataContext = allData.myData;
}

The combobox xaml in the MainWindow:

<ComboBox x:Name="comboBox" SelectedValue="{Binding myVar}" HorizontalAlignment="Left" Margin="193,139,0,0" VerticalAlignment="Top" Width="120"/>

The Constructor of another window (eg. Window1) where you want to show the selected value of your combobox should look like this:

public Window1()
{
     InitializeComponent();
     DataContext = allData.myData;
}

For example if you want to show the selected value of the combobox as a TextBlock the xaml in this case looks like this:

<TextBlock x:Name="textBlock" Text="{Binding myVar}" HorizontalAlignment="Left" Margin="96,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33