I am trying to change a List
that bind to DataGrid
from MainWindow
(The List is in another class).
How do i do it?
The List
contains Tuple
with 2 strings
and i want to change just the second string
when the user enters new values to a textBox
in a dialog box (that he open after clicking on a button in the MainWindow
.
(At the end i want the user to enter values in this dialog box and then show it in a list in another page (I mean another user control) so im open to hear also about different ways then this list of tuple bind to DataGrid
).
How do i change this string from another page (I mean from the MainWindow
)?
I tried to use MVVM the problem is that i dont find any connection between the mainWindow and the user control.
Name_List.xaml.cs:
namespace MyApp.Pages.SomeLists
{
public partial class Name_List: UserControl
{
private List<Tuple<string, string>> myNamesList = new List<Tuple<string, string>>();
public Name_List()
{
InitializeComponent();
myNamesList.Add(new Tuple<string, string>("First Name", your_name_bind));
myNamesList.Add(new Tuple<string, string>("Last Name", your_lastName_bind));
NamesGrid.ItemsSource = myNamesList;*/
}
}
}
Name_List.xaml:
<UserControl x:Class="MyApp.Pages.SomeLists.Name_List"
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"
xmlns:local="clr-namespace:MyApp.Pages.SomeLists"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DataGrid Name="NamesGrid" CanUserReorderColumns="True" CanUserResizeColumns="False" CanUserResizeRows="False" HorizontalAlignment="Left"
GridLinesVisibility="All">
</DataGrid>
</Grid>
</UserControl>
MainWindow.xaml:
<mui:ModernWindow x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
Title="App" IsTitleVisible="True"
Width="1200"
ContentSource="/Pages/Home.xaml">
<mui:ModernWindow.MenuLinkGroups>
<mui:LinkGroup DisplayName="Home page">
<mui:LinkGroup.Links>
<mui:Link DisplayName="home" Source="/Pages/Home.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
<mui:LinkGroup DisplayName="Lists" >
<mui:LinkGroup.Links>
<mui:Link DisplayName="Open Lists" Source="/Pages/MainLists.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
<mui:LinkGroup DisplayName="settings" GroupKey="settings">
<mui:LinkGroup.Links>
<mui:Link DisplayName="software" Source="/Pages/SettingsPage.xaml" />
</mui:LinkGroup.Links>
</mui:LinkGroup>
</mui:ModernWindow.MenuLinkGroups>
<mui:ModernWindow.TitleLinks>
<mui:Link x:Name="connect" DisplayName="connect"/>
<mui:Link DisplayName="settings" Source="/Pages/SettingsPage.xaml" />
<mui:Link DisplayName="help" Source="https://github.com" />
</mui:ModernWindow.TitleLinks>
</mui:ModernWindow>
MainWindow.xaml.cs
--There is a long code here... the relevant part is "connect_fun
()" function that called after the user click on "OK" in the dialog that opened to him after he clicked on "connect" tab. the dialog contains 2 textBox.--
namespace MyApp
public partial class MainWindow : ModernWindow
{
private string name = "";
private string lastName = "";
//Here i create a Modern dialog in code.
.....
TextBox name_txt = new TextBox();
TextBox lastName_txt = new TextBox();
public MainWindow()
{
InitializeComponent();
}
private void connect_fun(object sender, RoutedEventArgs e)
{
name= name_txt.Text;
lastName = lastName_txt_txt.Text;
}
}
}
MainLists.xaml:
<UserControl x:Class=MyApp.Pages.MainLists"
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"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:local="clr-namespace:MyApp.Pages"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Pages/Styles/BaseButtonStyle2.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
...
</Grid>
<Grid Style="{StaticResource ContentRoot}" Grid.Row="1">
<mui:ModernTab SelectedSource="/Pages/SomeLists/Name_List.xaml" Layout="Tab" >
<mui:ModernTab.Links>
<mui:Link DisplayName="Name List" Source="/Pages/SomeLists/Name_List.xaml"/>
<mui:Link DisplayName="Cars List" Source="/Pages/SomeLists/Cars_List.xaml"/>
</mui:ModernTab.Links>
</mui:ModernTab>
</Grid>
</Grid>
</UserControl>
I want to bind name and lastName to the dataGrid in Name_List.xaml (it means to your_name_bind
and your_lastName_bind
in myNamesList in my case).