弹出窗口中的Datagrid绑定 - 使用Caliburn.Micro的WPF MVVM

时间:2017-12-02 14:30:17

标签: wpf binding popup caliburn.micro

我正在尝试将数据网格插入到弹出窗口中,但它无法正常工作(弹出窗口包含空数据网格)。我试图将我的数据网格放在弹出窗口之外并且它已经工作了。我想popup的行为类似于单独的窗口,所以我想知道我是否应该为我的弹出窗口创建另一个ViewModel类,还是有其他方法可以解决这个问题?

我的查看xaml代码:

<UserControl x:Class="MyApplication.Views.AddArrivalView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-MyApplication.Views" >
<Grid>
    <StackPanel Orientation="Horizontal" Margin="5">
    //<Some textblocks>
    </StackPanel>
    <Popup Width="700" Height="300" IsOpen="true">
        <DataGrid x:Name="SuggestedCars"
          AutoGenerateColumns="true"
          IsReadOnly="True">
        </DataGrid>
    </Popup>
</Grid>
</UserControl>

我的ViewModel

namespace MyApplication.ViewModels
{
    class AddArrivalViewModel : PropertyChangedBase
    {
        private BindableCollection<Cars> _suggestedCars;

        public BindableCollection<Cars> SuggestedCars
        {
            get
            {
                return _suggestedCars;
            }
            set
            {
                _suggestedCars = value;
                NotifyOfPropertyChange("SuggestedCars");
            }
        }

        public AddArrivalViewModel()
        {
            SuggestedCars = new BindableCollection<Cars>();
            //add some test cars to collection 
            SuggestedCars.Add(new Cars() { Brand = "Opel", Model = "Corsa", Registration = "000000", ID = 0 });    
        }
    }
}

0 个答案:

没有答案