从WPF中的MainWindow更改列表

时间:2018-02-12 12:50:35

标签: wpf datagrid bind mainwindow

我正在尝试从List更改绑定到DataGrid的{​​{1}}(列表位于另一个类中)。 我该怎么做? MainWindow包含List,其中包含Tuple,当用户在对话框中向strings输入新值时,我想更改第二个string(他在点击textBox中的按钮后打开了。

(最后,我希望用户在此对话框中输入值,然后将其显示在另一个页面的列表中(我的意思是另一个用户控件),所以我也可以听到关于这个元组绑定列表的不同方式到MainWindow)。

如何从其他页面更改此字符串(我的意思是来自DataGrid)? 我试图使用MVVM的问题是我在mainWindow和用户控件之间找不到任何连接。

Name_List.xaml.cs:

MainWindow

Name_List.xaml:

    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;*/
            }

        }
    }

MainWindow.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.cs

- 这里有一个很长的代码......相关的部分是“<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> ()”函数,在用户点击“OK”后,在他点击“对话后打开给他的对话框中调用”连接“选项卡。该对话框包含2个textBox .--

connect_fun

MainLists.xaml:

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;

        }

    }
}

我想将name和lastName绑定到Name_List.xaml中的dataGrid(在我的情况下,它意味着myNamesList中的<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> your_name_bind。)

0 个答案:

没有答案