Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组合框项目资源的WPF不同数据上下文_Wpf_Mvvm_Combobox - Fatal编程技术网

组合框项目资源的WPF不同数据上下文

组合框项目资源的WPF不同数据上下文,wpf,mvvm,combobox,Wpf,Mvvm,Combobox,我有一个ItemControl,里面有一个comboBox,我试图实现的是为comboBox itemsSource提供一个不同的dataContext 这是我的itemControl: <ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" > <ItemsControl.ItemTemplate> <DataTemplate>

我有一个ItemControl,里面有一个comboBox,我试图实现的是为comboBox itemsSource提供一个不同的dataContext

这是我的itemControl:

<ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                    <DockPanel Margin="8">
                        <Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
                            <StackPanel >
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Name:" FontSize="15" FontWeight="SemiBold"/>
                                    <TextBox Text="{Binding Name}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Type:" FontSize="15" FontWeight="SemiBold"/>
                                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.EmployeeStatus}"
                                              SelectedValue="{Binding Type}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Units:" FontSize="15" FontWeight="SemiBold"/>
                                    <TextBox Text="{Binding Units}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Range:" FontSize="15" FontWeight="SemiBold"/>
                                    <TextBox Text="{Binding Range}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Scale:" FontSize="15" FontWeight="SemiBold"/>
                                    <TextBox Text="{Binding Scale}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Reason:" FontSize="15" FontWeight="SemiBold"/>
                                    <TextBox Text="{Binding Reason}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="10">
                                    <TextBlock Text="Description:" FontSize="15" FontWeight="SemiBold" />
                                    <TextBox Text="{Binding Description}" Width="70" Margin="10,0,0,0"/>
                                </StackPanel>
                            </StackPanel>
                        </Border>
                    </DockPanel>
                </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我看到了类似的情况,但无法与我的问题联系起来

解决方案1:每个组合框都有不同的项

必须使用ComboBox项源创建适当的类型

例如:

public class MyType
{
    public string Name { get; set; }
    public List<string> Types { get; set; }
    public string Type { get; set; }
    //...
}
公共类MyType
{
公共字符串名称{get;set;}
公共列表类型{get;set;}
公共字符串类型{get;set;}
//...
}
现在,您可以将ComboBox项源绑定到不同的源:

<ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DockPanel Margin="8">
                <Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
                    <StackPanel >
                        <StackPanel Orientation="Horizontal" Margin="10">
                            <TextBlock Text="Name:" FontSize="15" FontWeight="SemiBold"/>
                            <TextBox Text="{Binding Name}" Width="70" Margin="10,0,0,0"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="10">
                            <TextBlock Text="Type:" FontSize="15" FontWeight="SemiBold"/>
                            <ComboBox ItemsSource="{Binding Path=Types}"
                                      SelectedValue="{Binding Type}" Width="70" Margin="10,0,0,0"/>
                        </StackPanel>
                        ...
</ItemsControl>

...
您可以找到示例解决方案

解决方案2:所有组合框都有相同的项

公共类MyType
{
公共字符串名称{get;set;}
公共字符串类型{get;set;}
}
类MyViewModel
{        
私有列表类型;
公共列表类型
{
获取{return\u types;}
设置{u types=value;}
}
公共列表类型SiteMsource{get;set;}
}
XAML代码:

<ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" >
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <DockPanel Margin="8">
            <Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
                <StackPanel >
                    <StackPanel Orientation="Horizontal" Margin="10">
                        <TextBlock Text="Name:" FontSize="15" FontWeight="SemiBold"/>
                        <TextBox Text="{Binding Name}" Width="70" Margin="10,0,0,0"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="10">
                        <TextBlock Text="Type:" FontSize="15" FontWeight="SemiBold"/>
                            <ComboBox ItemsSource="{Binding Path=DataContext.TypesItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
                                  SelectedValue="{Binding Type}" Width="70" Margin="10,0,0,0"/>
                    </StackPanel>
                    ...
</ItemsControl>

...
您可以找到示例解决方案。

我使用“标记接口”来查找正确的数据上下文

public interface IDataContextMarker{} //just an empty interface
因此,在视图中,您的viewmodel必须实现此接口

public class MyView : IDataContextMarker {}
现在,您可以使用带有relativesource绑定的接口,而不是控件或ancestorlevel,这在某些情况下使事情变得更简单:)


这是什么意思?ItemsSource=“{Binding Path=Types}”路径是我的答案?@Yogevnn这意味着每个
ComboBox
在源对象中都有自己的ItemsSource。但我无法更改整个itemsControl引用的对象。我需要更改组合框的dataContext,以便它引用我的视图模型和模型object@Yogevnn每个组合框将具有不同的项目源,或者所有组合框将具有来自ViewModel的相同项目源?@Yogevnn再次检查我的答案:)
public class MyView : IDataContextMarker {}
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:IDataContextMarker}}, Path=DataContext.EmployeeStatus}"