Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用来自不同窗口的ObservableCollection发出_C#_Wpf_Xaml - Fatal编程技术网

C# 使用来自不同窗口的ObservableCollection发出

C# 使用来自不同窗口的ObservableCollection发出,c#,wpf,xaml,C#,Wpf,Xaml,当用户想要添加新提醒时,单击主窗口上的添加按钮;一旦他们添加了数据,它应该使用一个可观察的集合在主窗口的列表框中显示它 </MenuItem> <MenuItem Header="About"> <MenuItem Header="Info"/> </MenuItem> </Menu> <Button Content="New" HorizontalAlignment="Left" Hei

当用户想要添加新提醒时,单击主窗口上的添加按钮;一旦他们添加了数据,它应该使用一个可观察的集合在主窗口的列表框中显示它

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
这将打开一个新窗口,其中显示当前日期和消息的选项

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
当用户输入数据时,调用Finish方法

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
问题是,当用户在新窗口上输入完数据后,我将其添加到提醒收集中,但它不会在主窗口上更新。我想知道这是否是一个datacontext问题,我是否采取了正确的方法

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
谢谢你的帮助

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
添加窗口:

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class AddWindow : Window, INotifyPropertyChanged
    {

        private MainWindow mainW;

        public AddWindow(MainWindow mW)
        {
            InitializeComponent();

            mainW = mW;
            this.Show();

            DataContext = this;
        }

        private void Finish(object sender, RoutedEventArgs e)
        {
            mainW.Reminders.Add(new Remind(SelectedDate, Message));
            this.Close();
        }


        private DateTime selectedDate = DateTime.Today;

        public DateTime SelectedDate
        {
            get
            {
                return selectedDate;
            }
            set
            {
                if (value != selectedDate)
                {
                    selectedDate = value;
                    RaisePropertyChange("SelectedDate");
                }
            }
        }


        private string message;

        public string Message
        {
            get
            {
                return message;
            }

            set
            {
                if (message != value)
                {
                    message = value;
                    RaisePropertyChange("Message");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChange(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>

嗯,我用你的代码创建了一个小的解决方案,效果很好。单击“完成”后,主窗口的列表将立即更新。唯一的小问题是在AddWindow中对消息使用了错误的绑定。您绑定到“Msg”,但在上面第3行中它应该是“Message”:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
        <TextBox  Text="{Binding Message}" HorizontalAlignment="Left" Height="58" Margin="123,93,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="144"/>

Hm,我用您的代码创建了一个小型解决方案,效果很好。单击“完成”后,主窗口的列表将立即更新。唯一的小问题是在AddWindow中对消息使用了错误的绑定。您绑定到“Msg”,但在上面第3行中它应该是“Message”:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
        <TextBox  Text="{Binding Message}" HorizontalAlignment="Left" Height="58" Margin="123,93,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="144"/>

Hm,我用您的代码创建了一个小型解决方案,效果很好。单击“完成”后,主窗口的列表将立即更新。唯一的小问题是在AddWindow中对消息使用了错误的绑定。您绑定到“Msg”,但在上面第3行中它应该是“Message”:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
        <TextBox  Text="{Binding Message}" HorizontalAlignment="Left" Height="58" Margin="123,93,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="144"/>

Hm,我用您的代码创建了一个小型解决方案,效果很好。单击“完成”后,主窗口的列表将立即更新。唯一的小问题是在AddWindow中对消息使用了错误的绑定。您绑定到“Msg”,但在上面第3行中它应该是“Message”:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
        <TextBox  Text="{Binding Message}" HorizontalAlignment="Left" Height="58" Margin="123,93,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="144"/>

代码看起来不错,但我可以从中看出一个问题:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
AddWindow
中的文本框与
Msg
绑定,但代码隐藏中相应的属性名称是
消息
。因此,文本框永远不会被正确绑定,因此在集合中添加了带有
String.Empty
Msg
值的新项

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBox  Text="{Binding Msg}" <-- HERE. It should be Message.

代码看起来不错,但我可以看到其中的一个问题:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
AddWindow
中的文本框与
Msg
绑定,但代码隐藏中相应的属性名称是
消息
。因此,文本框永远不会被正确绑定,因此在集合中添加了带有
String.Empty
Msg
值的新项

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBox  Text="{Binding Msg}" <-- HERE. It should be Message.

代码看起来不错,但我可以看到其中的一个问题:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
AddWindow
中的文本框与
Msg
绑定,但代码隐藏中相应的属性名称是
消息
。因此,文本框永远不会被正确绑定,因此在集合中添加了带有
String.Empty
Msg
值的新项

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBox  Text="{Binding Msg}" <-- HERE. It should be Message.

代码看起来不错,但我可以看到其中的一个问题:

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
AddWindow
中的文本框与
Msg
绑定,但代码隐藏中相应的属性名称是
消息
。因此,文本框永远不会被正确绑定,因此在集合中添加了带有
String.Empty
Msg
值的新项

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBox  Text="{Binding Msg}" <-- HERE. It should be Message.

将主窗口中的dateT更改为dateT

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBlock Text="{Binding Path=DateT}"/>

你就完了

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>

在底线之下,datacontext的一切都是正常的。您的两个错误属性名称拼写错误。

在主窗口中将dateT更改为dateT

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBlock Text="{Binding Path=DateT}"/>

你就完了

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>

在底线之下,datacontext的一切都是正常的。您的两个错误属性名称拼写错误。

在主窗口中将dateT更改为dateT

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBlock Text="{Binding Path=DateT}"/>

你就完了

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>

在底线之下,datacontext的一切都是正常的。您的两个错误属性名称拼写错误。

在主窗口中将dateT更改为dateT

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
<TextBlock Text="{Binding Path=DateT}"/>

你就完了

    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>


在底线之下,datacontext的一切都是正常的。您的两个错误的属性名称拼写错误。

似乎对我的仍然不起作用,您是否使用mainW.members.Add(新提醒(SelectedDate,Message));要添加到集合中吗?你是怎么处理数据上下文的?很奇怪。我正在运行您的代码,没有任何修改(除了前面提到的代码)。当然,我需要添加提醒类(这只是一个小容器,其中包含dateT和Msg)。DataContext的一切都很好。只是一个想法:是否以某种方式覆盖XAML中的DataContext?可能是通过设置ScrollView的父数据上下文来实现的?我只是把你的代码放到主窗口的网格中。在底部添加了我版本的提醒类,看起来还可以吗?再次查看我的xaml,主窗口在上面发布,似乎没有在任何地方设置数据上下文。似乎仍然不适用于我的,你使用的是mainW.members.Add(新提醒(SelectedDate,Message));要添加到集合中吗?你是怎么处理数据上下文的?很奇怪。我正在运行您的代码,没有任何修改(除了前面提到的代码)。当然,我需要添加提醒类(这只是一个小容器,其中包含dateT和Msg)。DataContext的一切都很好。只是一个想法:是否以某种方式覆盖XAML中的DataContext?可能是通过设置ScrollView的父数据上下文来实现的?我只是把你的代码放到主窗口的网格中。在底部添加了我版本的提醒类,看起来还可以吗?再次查看我的xaml,主窗口在上面发布,似乎没有在任何地方设置数据上下文。似乎仍然不适用于我的,你使用的是mainW.members.Add(新提醒(SelectedDate,Message));要添加到集合中吗?你是怎么处理数据上下文的?很奇怪。我正在运行您的代码,没有任何修改(除了前面提到的代码)。属于
    </MenuItem>
    <MenuItem Header="About">
        <MenuItem Header="Info"/>
    </MenuItem>
</Menu>
<Button  Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
    <ListBox ItemsSource= "{Binding Reminders}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="41" Width="293" >
                    <TextBlock Text="{Binding Path=dateT}"/>
                    <TextBlock Text="{Binding Path=Msg}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>