0

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).

Ben
  • 201
  • 1
  • 4
  • 13
  • 1
    I would **highly recommend** to read about MVVM and WPF. First things first your UC is completely not needed, just use the `DataGrid` instead. If you wopuld use a ViewMdoel this wouldn't be a problem. Have you been coding in `WinForms` prior to WPF? Also `Tuple` can be easily replaced by a class with INPC. – XAMlMAX Feb 12 '18 at 13:19
  • Well i also worked with MVVM in my project, but it doesn't matter here because i can't find a way to change a list from another page (not the one that its dataContext "connected" to the ViewModel. – Ben Feb 12 '18 at 13:24
  • You need a reference to your `Name_List` control. Are you using it in the `MainWindow` anyway? – XAMlMAX Feb 12 '18 at 13:27
  • Yes i use it anyway. The problem is that i can't create an instance of Name_List control because it's a page and i get it when i click at the link to it... (I mean i can't create an instance of Name_List in MainWindow and connect both to the same viewModel). @XAMlMAX – Ben Feb 12 '18 at 13:45
  • If you can't use it then how do you reference it in your xaml? – XAMlMAX Feb 12 '18 at 13:51
  • Oh no sorry, i didn't read your sentence properly. No i don't use it in my MainWindow... – Ben Feb 12 '18 at 13:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164968/discussion-between-ben-and-xamlmax). – Ben Feb 12 '18 at 13:59

0 Answers0