使用Caliburn.Micro的MVVM更新ComboBox SelectedItem的ListView

时间:2018-06-25 18:09:04

标签: c# wpf combobox

我有一个具有主组合框的视图,当用户选择一个项目时,它将使用选定的值更新所有子组合框。目标是允许用户立即更改文件的所有行业/所有者,或者能够使用子组合框单独更改文件的行业/所有者。

我已经在视图模型中创建了事件以侦听主要更改,但是如何更新子组合框?子组合框嵌套在ListView中。

XAML

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Name="IndustryWidth"/>
                <ColumnDefinition x:Name="OwnerWidth"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <Label HorizontalAlignment="Center" VerticalAlignment="Bottom">Master Industry</Label>

            <ComboBox x:Name="MasterIndustry" Grid.Row="1" VerticalAlignment="Bottom" SelectedItem="{Binding SelectedObjectIndustry}">
                <ComboBoxItem Content="HVAC"></ComboBoxItem>
                <ComboBoxItem Content="Plumbing"></ComboBoxItem>
                <ComboBoxItem Content="Roofing"></ComboBoxItem>
                <ComboBoxItem Content="Electrician"></ComboBoxItem>
            </ComboBox>


            <Label Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Bottom">Master Owner</Label>

            <ComboBox x:Name="MasterOwner" Grid.Column="1" Grid.Row="1" VerticalAlignment="Bottom" SelectedValue="{Binding SelectedObjectOwner}" SelectedValuePath="Content">
                <ComboBoxItem Content="Jason"></ComboBoxItem>
                <ComboBoxItem Content="Mike"></ComboBoxItem>
                <ComboBoxItem Content="Keri"></ComboBoxItem>
            </ComboBox>
        </Grid>

        <ListView x:Name="FileListView" ItemsSource="{Binding Files, Mode=OneWay}" Background="LightBlue" Margin="5" Grid.Row="1" HorizontalAlignment="Stretch">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="File Name" DisplayMemberBinding="{Binding FileName}" />
                    <GridViewColumn Header="Industry" Width="Auto" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Width="100" HorizontalAlignment="Right" >
                                  <ComboBoxItem Content="HVAC"></ComboBoxItem>
                                  <ComboBoxItem Content="Plumbing"></ComboBoxItem>
                                  <ComboBoxItem Content="Roofing"></ComboBoxItem>
                                  <ComboBoxItem Content="Electrician"></ComboBoxItem>
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Owner" Width="auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="IndividualOwner" Width="100" HorizontalAlignment="Right" SelectedItem="{Binding SelectedIndividualOwner}">
                                    <ComboBoxItem Content="Jason"></ComboBoxItem>
                                    <ComboBoxItem Content="Mike"></ComboBoxItem>
                                    <ComboBoxItem Content="Keri"></ComboBoxItem>
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
   </Grid>
 </UserControl>

查看模型

除了LoadFileView()之外,我不确定如何将IndividualOwner控件绑定到主控件上该怎么做。

  public FileViewModel()
    {
        LoadFileView();
    }

    private void LoadFileView()
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "CSV Files (*.CSV)|*.CSV|All Files (*.*)|*.*";


        if (openFileDialog.ShowDialog() == true)
        {
            foreach (string file in openFileDialog.FileNames)
            {
                Files.Add(new FileModel(file));
            }
        }

    }             

    public BindableCollection<FileModel> Files { get; set; } = new BindableCollection<FileModel>();

    private string _selectedObjectIndustry;

    public string SelectedObjectIndustry
    {
        get
        {
            return _selectedObjectIndustry;
        }
        set
        {
            if (_selectedObjectIndustry == value)
            {
                return;
            }

            _selectedObjectIndustry = value;
        }
    }

    private string _selectedObjectOwner;

    public string SelectedObjectOwner
    {
        get
        {
            return _selectedObjectOwner;
        }
        set
        {
            if (_selectedObjectOwner == value)
            {
                return;
            }

            _selectedObjectOwner = value;
            MasterSelectionUpdate();
        }
    }

    public string SelectedIndividualOwner
    {
        get
        {
            return _selectedObjectOwner;
        }
        set
        {
            if (_selectedObjectOwner == value)
            {
                return;
            }

            _selectedObjectOwner = value;
            NotifyOfPropertyChange(() => SelectedIndividualOwner);
        }
    }

0 个答案:

没有答案