WPF ComboBox将itemssource绑定到MVVM中的不同数据文件 [英] WPF ComboBox bind itemssource to different datacontext in MVVM

查看:400
本文介绍了WPF ComboBox将itemssource绑定到MVVM中的不同数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,其数据报文设置为一个视图模型 EmployeeList_VM 。然后,我在该用户控件中有一个ContentControl,其数据访问设置为视图模型的公共属性( EmployeeSelection )。 ContentControl的数据文本与同一用户控件中列表框的选定项绑定相同。

I have a user control that has its datacontext set to a view model called EmployeeList_VM. I then have a ContentControl within that user-control that has its datacontect set to a public property (EmployeeSelection) of the view model. The ContentControl's datacontext is the same as the selected item binding of a listbox within the same user control.

我想要一个组合框(一个名为 EmployeeStatus )将其所选项目与ContentControl的 EmployeeSelection 数据报文的属性绑定。我想将组合框的itemssource绑定到parentviewmodel EmployeeList_VM 的公共属性 EmployeeStatus

I want to have a combobox (of an observblecollection called EmployeeStatus) within the ContentControl bind its selecteditem to a property of the EmployeeSelection datacontext of the ContentControl. I want the itemssource of the combobox to bind to a public property EmployeeStatus of the "parent" viewmodel EmployeeList_VM.

我可以得到一个列表的员工状态出现在组合框。我无法将其绑定到 EmployeeSelection 的idStatus属性。

I can get a list of the employees status to appear wihtin the combobox. I can not get it to bind to the idStatus property of the EmployeeSelection.

这里是我到目前为止):

Here is What I have so far (some code removed for readability):

<UserControl x:Class="FTC.View.EmployeeListView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            xmlns:local="clr-namespace:FTC_Application"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:cmd="http://www.galasoft.ch/mvvmlight"
            mc:Ignorable="d" 
            DataContext="{Binding EmployeeList_VM, Source={StaticResource Locator}}"
            d:DesignHeight="900" d:DesignWidth="1000">

        <Expander x:Name="DetailExpander" Grid.Column="2" Header="employee detail" Style="{DynamicResource ExpanderStyle_FTC}" IsExpanded="True" Padding="8,0,0,10" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Left" >
                    <Button Content="" Style="{DynamicResource ButtonSave}" Command="{Binding SaveCommand}" ToolTip="Save Changes" Margin="0,0,10,10"/>
                    <Button Content="" Style="{DynamicResource ButtonUndo}" Command="{Binding UndoCommand}" ToolTip="Undo Changes" Margin="0,0,10,10"/>
                </StackPanel>
                <ScrollViewer Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <ContentControl DataContext="{Binding Path=EmployeeSelection, Mode=TwoWay}" >
                            <!-- FTC SETTINGS GRID CONTENT-->
                            <Grid Grid.ColumnSpan="6" Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="30"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <TextBlock x:Name="SettingsHeading" Text="FTC Settings" Style="{StaticResource FTC_DetailHeading}" Grid.ColumnSpan="5"/>

                                <TextBlock Text="Status" Style="{StaticResource FTC_DetailLabelSub}" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"/>
                                <ComboBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2"
                                          Margin="5,5,16,5" Height="37"
                                          ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.EmployeeStatus}"
                                          SelectedItem="{Binding idStatus, Mode=TwoWay}"
                                          SelectedValuePath="idStatus"
                                          DisplayMemberPath="chrCode" FontSize="18"/>

                           </Grid>
                    </ContentControl>
                </ScrollViewer>
            </Grid>
        </Expander>
</UserControl>

我甚至为组合框的所选项尝试了以下更改(我命名为ContentControl DetailControl):

I even tried the following change (I named the ContentControl DetailControl) for the selected item of the combobox:

SelectedItem="{Binding ElementName=DetailControl, Path=DataContext.idStatus}"

有人可以帮我将所选择的项目绑定正确连接。组合框显示所有正确的项目,但它们不绑定到EmployeeSelection.idStatus。

Can someone please help me get the selected item binding hooked up properly. The combobox displays all the right items, but they do not bind to the EmployeeSelection.idStatus.

提前感谢。

推荐答案

所以这里是对我有用,希望它可以帮助别人

so here is what worked for me, hope it can help someone else

<ComboBox Name="cbStatus" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2"
                                          Style="{StaticResource FTC_DetailComboBox}"
                                          ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.EmployeeStatus}"
                                          SelectedItem="{Binding ElementName=DetailControl, Path=DataContext.employee_status}"
                                          SelectedValuePath="idStatus"
                                          SelectedValue="{Binding idStatus, Mode=TwoWay, ValidatesOnDataErrors=True}"
                                          DisplayMemberPath="chrCode"/>

这篇关于WPF ComboBox将itemssource绑定到MVVM中的不同数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆