6

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

我想在 ContentControl 中有一个组合框(一个名为EmployeeStatus的 observblecollection )将其选定项绑定到 ContentControl 的 EmployeeSelection 数据上下文的属性。我希望组合框的 itemssource 绑定到“父”视图模型EmployeeList_VM的公共属性EmployeeStatus

我可以获取显示在组合框中的员工状态列表。我无法将其绑定到EmployeeSelection的 idStatus 属性。

这是我到目前为止所拥有的(为了便于阅读,删除了一些代码):

<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):

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

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

提前致谢。

4

1 回答 1

9

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

<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"/>
于 2013-02-09T23:35:37.337 回答