【问题标题】:How do I bind a comboBox to a different ItemsSource than it's parent DataGrid's ItemSource in a UWP project?如何在 UWP 项目中将组合框绑定到与其父 DataGrid ItemSsource 不同的 ItemsSource?
【发布时间】:2020-12-11 10:32:24
【问题描述】:

我正在使用 MVVM 并将 DataGrid 绑定到 ObservableCollection<CustomClass>。我可以成功地做到这一点,但是我希望DataGrid 列之一是ComboBox 列,项目源为List<ComboBoxValues>。此外,另一列必须是 ToggleSwitch,其中带有参数的 CommandIsOn 属性更改值时被触发。

在 ViewModel 中:

    public ObservableCollection<CustomClass> DataGridData { get; set; } = new ObservableCollection<CustomClass>();
    public List<ComboBoxValues> ListValues { get; set; } = new List<ComboBoxValues>();
    public MyICommand<ToggleSwitch> ToggleSwitchToggled_Command { get; private set; }

我遇到的问题是DataGridItemsSource Binding 覆盖了CombobBoxItemsSourceBinding 路径以及Command 的@ 987654339@。在DataGridData 中查找ListValuesToggleSwitchToggled_Command

例如:

错误:BindingExpression 路径错误:在“UWPProject.ViewModels.DataGridData”上找不到“ListValues”属性。 BindingExpression: Path='ListValues' DataItem='UWPProject.ViewModels.DataGridData';目标元素是'Windows.UI.Xaml.Controls.ComboBox'(名称='null');目标属性是“ItemsSource”(类型“对象”)

在 Xaml 中:

      <controls:DataGrid GridLinesVisibility="All"
                         AlternatingRowBackground="Gray" AutoGenerateColumns="False"
                         ItemsSource="{Binding DataGridData,Mode=TwoWay}">
            <controls:DataGrid.Columns>
                <controls:DataGridTextColumn Header="TextOne" Binding="{Binding aPropertyOneInDataGridData,Mode=TwoWay}"/>
                <controls:DataGridTextColumn Header="TextTwo" Binding="{Binding aPropertyTwoInDataGridData,Mode=TwoWay}"/>
                <controls:DataGridTemplateColumn Header="ComboBoxHeader">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <ComboBox ItemsSource="{Binding ListValues}"
                                          SelectedValue="{Binding aPropertyThreeInDataGridData,Mode=TwoWay}"
                                          PlaceholderText="Select Action">
                                </ComboBox>
                            </StackPanel>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
                <controls:DataGridTemplateColumn Header="ToggleSwitch_Header">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <ToggleSwitch x:Name="ToggelSwitch_Run"
                                              IsOn="{Binding aPropertyFourInDataGridData,Mode=TwoWay}">
                                    <interact:Interaction.Behaviors>
                                        <interactcore:EventTriggerBehavior EventName="Toggled">
                                            <interactcore:InvokeCommandAction Command="{Binding ToggleSwitchToggled_Command}"
                                                                              CommandParameter="{Binding}"/>
                                        </interactcore:EventTriggerBehavior>
                                    </interact:Interaction.Behaviors>
                                </ToggleSwitch>
                            </StackPanel>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
            </controls:DataGrid.Columns>
        </controls:DataGrid>

所以我想,问题是,如何拆分在父 DataGrid 中查找 Binding 的“源”或路径。这方面有很多WPF的问答,比如herehere。但是我不能使用DataContext,因为我收到“DataContext is not supported in a UWP project”的错误。我找不到针对此问题的 UWP 项目的解决方案。另外,我使用的是 MVVM 并且没有后面的代码,所以我不相信我可以使用x:Binding。我也不想在后面使用代码。

请帮忙。

【问题讨论】:

  • 以下答案是否有效,您有任何更新吗?

标签: c# xaml uwp datagrid datagridtemplatecolumn


【解决方案1】:

对于您的场景,我们建议使用DataGridComboBoxColumn 替换自定义单元格模板。然后您可以使用 x:bind 标记扩展直接绑定列表属性。 因为DataGridComboBoxColumnItemsSource属性不能直接访问数据源子属性,如果你已经为Page类创建了ListValues属性,你也可以像下面这样使用x:bind来访问。

<controls:DataGridComboBoxColumn 
    Width="*"
    Binding="{Binding p2}"
    Header="Link"   
    ItemsSource="{x:Bind ListValues ,Mode=OneWay}"
    Tag="Link"
    />

如果您确实想制作自定义单元格模板,您可以将 ListValues 属性插入您的CustomClass。更多详情请参考this case reply

【讨论】:

  • 看来我不能在页面的 .cs 文件中没有 code-behind/ 属性的情况下使用 x:bind。我想严格使用 MVVM,因此没有代码隐藏。因此我不能使用 x:bind。我的代码使用绑定,对于 DataGridComboBoxColumn,现在唯一不起作用的是,加载时没有值,下拉列表中没有值,它保持为空
  • 很好,你可以给你的视图模型一个 x:name 并使用 x:bind 来绑定特定的属性。
  • 需要将我的 ListValues 设为静态才能正常工作,是否可以将 x:bind 绑定到非静态列表?那么我也需要一个静态命令,它也会给我带来问题吗?
  • 你能不能发新帖来描述你的问题,在你的cmets中很难解决你的问题。
  • 嗨@NicoZhu,我发布了另一个问题Inline Link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 2014-02-15
  • 1970-01-01
  • 2018-07-16
  • 2017-09-18
  • 1970-01-01
  • 2013-01-14
相关资源
最近更新 更多