在XAML

时间:2017-09-27 20:46:34

标签: c# wpf xaml mvvm combobox

这是WPF MVVM应用程序的一部分。我相信我遇到的问题是数据上下文不清楚。此数据网格的ItemsSource是ViewModels的集合。这是ViewModel中用于显示其余数据的属性。因此,所有数据都显示在SpecimentViewModel中,甚至是单个项目。唯一不起作用的是我无法在comboBox中显示所选的选项。由于我一直在搞乱这个,我甚至看不到默认的“选择注释”(第0项)。正如我所说,所有其他列出现。它们是每个viewModel中的所有属性。那么我的ComboBox没有看到DataContext吗?

                <DataGrid
                ItemsSource="{Binding Specimens}"
                Style="{StaticResource DataGridStyle}"
                Background="Beige"
                FontFamily="Verdana"
                FontSize="10"
                AutoGenerateColumns="False"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                AlternatingRowBackground="Beige"
                AlternationCount="2"
                AllowDrop="False"
                CanUserAddRows="False"
                DataContext="">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding TestNumber}"
                                        Header="No."
                                        Width=".05*" />

                    <DataGridTextColumn Binding="{Binding PlyCount}"
                                        Header="Ply Count"
                                        Width=".14*" />

                    <DataGridTextColumn Binding="{Binding PlyThickness}"
                                        Header="Ply Thickness"
                                        Width=".14*" />

                    <DataGridTextColumn Binding="{Binding TearPerPly}"
                                        Width=".14*">

                        <DataGridTextColumn.Header>
                            <TextBlock Text="{Binding DataContext.SpencerDartHeading, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>

                    <DataGridTemplateColumn
                        Header="Note"
                        Width=".14*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding ResultOptions}"
                                          SelectedItem="{Binding SelectedNote, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                          SelectedValue="{Binding SelectedNote, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                          IsSynchronizedWithCurrentItem="True"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

ViewModel看起来像这样;

    private Specimen specimen;
    private ObservableCollection<string> resultOptions = new ObservableCollection<string>();
    public Specimen Specimen
    {
        get
        {
            return specimen;
        }
        set
        {
            specimen = value;
            RaisePropertyChanged("Specimen");
        }
    }

    public string SelectedNote
    {
        get
        {
            if (specimen.ResultNote == null)
                specimen.ResultNote = "Select Note";
            return specimen.ResultNote;
        }
        set
        {
            specimen.ResultNote = value;
            RaisePropertyChanged();
        }
    }

    public ObservableCollection<string> ResultOptions
    {
        get { return resultOptions; }
        set
        {
            resultOptions = value;
            RaisePropertyChanged();
        }
    }

Specimen Model只有一些测试属性和ResultNote字符串属性。因此,TestViewModel具有单个测试的属性,其中一个是名为Specimens的SpecimenViewModel的集合。每个SpecimenViewModels都有一个ResultNote属性。该结果注释是从组合框中选择的,该组合框填充了一组名为ResultOptions的选项,该选项位于SpeciimenModel中。选定的值将放入属性“ResultNote。”

5 个答案:

答案 0 :(得分:1)

试试这个:

<DataGridTemplateColumn Header="Note"
                        Width=".14*" 
                        SortMemberPath="SelectedNote">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding ResultOptions}"
                      SelectedItem="{Binding SelectedNote, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="SelectedNote" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

答案 1 :(得分:0)

在标头绑定中,尝试使用SelectedItem属性而不是DataContext

<TextBlock Text="{Binding SelectedItem.SpencerDartHeading, ..." />

答案 2 :(得分:0)

这是视图:

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid
                ItemsSource="{Binding Specimens}"

                Background="Beige"
                FontFamily="Verdana"
                FontSize="10"
                AutoGenerateColumns="False"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                AlternatingRowBackground="Beige"
                AlternationCount="2"
                AllowDrop="False"
                CanUserAddRows="False"
                >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding TestNumber}"
                                        Header="No."
                                        Width=".05*" />

                <DataGridTextColumn Binding="{Binding PlyCount}"
                                        Header="Ply Count"
                                        Width=".14*" />

                <DataGridTextColumn Binding="{Binding PlyThickness}"
                                        Header="Ply Thickness"
                                        Width=".14*" />

                <DataGridTextColumn Binding="{Binding TearPerPly}"
                                        Width=".14*">

                    <DataGridTextColumn.Header>
                        <TextBlock Text="{Binding DataContext.SpencerDartHeading, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <DataGridTemplateColumn Header="Note"
                        Width=".14*" 
                        SortMemberPath="SelectedNote">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding ResultOptions}"
                      SelectedItem="{Binding SelectedNote, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="SelectedNote" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

和ViewModel:

public class SpecimenViewModel
    {
        private Specimen specimen;
        private ObservableCollection<string> resultOptions = new ObservableCollection<string>();

        public SpecimenViewModel(Specimen specimen)
        {           
            resultOptions.Add("Select Note");
            resultOptions.Add("Select 1");
            resultOptions.Add("Select 2");
            resultOptions.Add("Select 3");

            this.specimen = specimen;
            RaisePropertyChanged("SelectedNote");
        }


        public Specimen Specimen
        {
            get
            {
                return specimen;
            }
            set
            {
                specimen = value;
                RaisePropertyChanged("Specimen");
            }
        }

        public string SelectedNote
        {
            get
            {
                if (specimen.ResultNote == null)
                    specimen.ResultNote = "Select Note";
                return specimen.ResultNote;
            }
            set
            {
                specimen.ResultNote = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<string> ResultOptions
        {
            get { return resultOptions; }
            set
            {
                resultOptions = value;
                RaisePropertyChanged();
            }
        }


        }

答案 3 :(得分:0)

我感谢大家的回答。尤其是卡斯帕和Ncfuncion。他们在答案中投入了大量的精力。不幸的是,他们都没有为我工作。事实证明我的XAML代码很好。我终于发现问题在于汇编数据。我无法解释为什么这是一个问题。我能够及时回到我知道它工作的时候(谢天谢地我使用GIT版本控制)。我一遍又一遍地检查代码,最后找到了一条我在路上改变的路线。我确信我已经调试并看到了正确的值,但它们并没有出现在数据网格中。哦,我恢复了大脑。对于想要查看工作代码在这里看起来是什么的任何人都是XAML。为简洁起见,我跳过了其他专栏。

                <DataGrid
                ItemsSource="{Binding Specimens}"
                Style="{StaticResource DataGridStyle}"
                Background="Beige"
                FontFamily="Verdana"
                FontSize="10"
                AutoGenerateColumns="False"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                AlternatingRowBackground="Beige"
                AlternationCount="2"
                AllowDrop="False"
                CanUserAddRows="False"
                >
                <DataGrid.Columns> 
                   <DataGridTemplateColumn
                        Header="Note"
                        Width=".14*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding ResultOptions}"
                                          SelectedItem="{Binding SelectedNote, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                          SelectedValue="{Binding SelectedNote}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

答案 4 :(得分:-1)

与为DataGrid中的最后一个文本列设置的内容类似,通过指定RelativeSource属性来使用数据绑定,您也可以使用这种方法为ComboBox设置ItemsSource,就像这样:

                <DataGridTemplateColumn
                    Header="Note"
                    Width=".14*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.ResultOptions}"
                                      SelectedItem="{Binding SelectedNote, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                      IsSynchronizedWithCurrentItem="True"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

至于SelectedValueSelectedItem,我认为您应该只设置其中一个。