绑定Listview SelectedItems for Actions

时间:2016-04-07 11:43:02

标签: c# wpf mvvm data-binding caliburn.micro

我正在努力将2个列表视图选定的项目绑定到我的viewmodel 我们的想法是拥有一个包含2个列表视图和两个按钮的视图,将(多个)所选项目从1个列表视图移动到另一个列表视图(2路)。

观点:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListView x:Name="DuamResources" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Name}"/>
            </GridView>
        </ListView.View>
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
    <StackPanel Grid.Column="1" Orientation="Vertical" cal:Action.TargetWithoutContext="{Binding SelectedDuamResources}" VerticalAlignment="Center">
        <Button Content="&lt;" Width="40" Margin="5" cal:Message.Attach="AddSelectedResources($datacontext)"/>
        <Button Content="&gt;" Width="40" Margin="5"/>
    </StackPanel>
    <ListView x:Name="AvailableDuamResources" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Name}"/>
            </GridView>
        </ListView.View>
        <ListView.GroupStyle>
            <GroupStyle HidesIfEmpty="True">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Type}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</Grid>

关联的viewmodel属性:

private ObservableCollection<DuamResourceInfo> availableDuamResources;
        public ObservableCollection<DuamResourceInfo> AvailableDuamResources
        {
            get { return availableDuamResources; }
            set { availableDuamResources = value; NotifyOfPropertyChange(() => AvailableDuamResources); }
        }

        private ObservableCollection<DuamResourceInfo> selectedDuamResources;
        public ObservableCollection<DuamResourceInfo> SelectedDuamResources
        {
            get { return selectedDuamResources; }
            set { selectedDuamResources = value; NotifyOfPropertyChange(() => SelectedDuamResources); }
        }

        private ObservableCollection<DuamResourceInfo> duamResources;
        public ObservableCollection<DuamResourceInfo> DuamResources
        {
            get { return duamResources; }
            set { duamResources = value; NotifyOfPropertyChange(() => DuamResources); }
        }

我想要实现的代码:

public void AddSelectedResources(IEnumerable<DuamResourceInfo> resources)
{

}

public void RemoveSelectedResources(IEnumerable<DuamResourceInfo> resources)
{

}

我正在为MVVM使用 Caliburn Micro 。我知道有一些约束约定,但我不确定它们适用于这种情况。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您是否尝试使用“x:Bind”而不是“Binding”?出于某种原因,过去某种原因对我有用,但不是两种,取决于具体情况。