首页 文章

在Material Design Dialog WPF中插入用户控件

提问于
浏览
0

我试图在WPF中理解xaml中的材料设计,此时我正在使用Dialog Host . 我试图将UserControl放在材料设计对话框主机中但不知何故它不起作用,我正在使用Caliburn Micro FW的材料设计 .

MainView.xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--ROW 0-->
    <Button Content="Show Dialog" Grid.Row="0" Grid.Column="0" x:Name="OpenDialog"/>
    <!--ROW 1-->
    <materialDesign:DialogHost Grid.Row="1" Grid.Column="0" IsOpen="{Binding IsDialogOpen}" CloseOnClickAway="True">
        <materialDesign:DialogHost.DialogContent>
            <StackPanel Margin="20">
                <ContentControl x:Name="ActiveItem"/>
            </StackPanel>
        </materialDesign:DialogHost.DialogContent>
    </materialDesign:DialogHost>
</Grid>

MainViewModel.cs

private bool _isDialogOpen;
    public bool IsDialogOpen
    {
        get { return _isDialogOpen; }
        set
        {
            _isDialogOpen = value;
            NotifyOfPropertyChange(() => IsDialogOpen);
        }
    }

    public LoginViewModel()
    {
        MyCustomers.Add("Dhairya Joshi");
        MyCustomers.Add("Irfan Shethia");
        MyCustomers.Add("Imran Shethia");
    }

    public void OpenDialog()
    {
        IsDialogOpen = true;
        ActivateItem(new CustomersListViewModel());
    }

CustomerView.xaml

<StackPanel Orientation="Vertical">
    <ListBox x:Name="MyCustomers"/>
</StackPanel>

CustomerViewModel.cs

private List<string> _myCustomers = new List<string>();
    public List<string> MyCustomers
    {
        get { return _myCustomers; }
        set {
            _myCustomers = value;
            NotifyOfPropertyChange(() => MyCustomers);
        }
    }

    public CustomersListViewModel()
    {
        MyCustomers.Add("Dhairya Joshi");
        MyCustomers.Add("Irfan Shethia");
        MyCustomers.Add("Imran Shethia");
    }

现在它只是显示像这个屏幕截图

enter image description here
.

注意:我试图将 <contentControl> 放在新行中,相同的代码确实可以正常工作,但是当我在DialogHost中使用它时它只是没有显示 . 试图删除StackPanel并只留下 <contentcontrol> 但仍然无法正常工作 .

更新:

最后,我能够将其置于工作状态,但又出现了另一个问题,即用户控件内的ListBox没有被填充 .

我如何能够在对话框中显示用户控件:

1)MainView.xaml

<materialDesign:DialogHost Grid.Row="2" Grid.Column="0" IsOpen="{Binding IsDialogOpen}" CloseOnClickAway="True" Identifier="RootDialog">

2)MainViewModel.cs

public async void OpenDialog()
    {
        //IsDialogOpen = true;


        var view = new CustomersListView
        {
            DataContext = new CustomerListViewModel();
        };

        //show the dialog
        var result = await DialogHost.Show(view, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
    }

    private void ExtendedOpenedEventHandler(object sender, DialogOpenedEventArgs eventargs)
    {
        Console.WriteLine("Detecting Opened Event");
    }
    private void ExtendedClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
    {

    }

如果我直接运行usercontrol然后listview填充,但是当我在对话框中执行它时它不会 . 不知道为什么 .

1 回答

相关问题