我想将内部ObservableCollection绑定到CollectionViewSource以对其应用过滤器。但不幸的是,我在第一步失败了。我无法将我的集合绑定到collectionViewSource作为新资源。我将以下代码替换为正常工作:
<DataGridTemplateColumn x:Key="MyComboBoxFunctionColumn" Header="Function">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Mux.Setting.FunctionList}" DisplayMemberPath="Name" SelectedIndex="{Binding Mux.Setting.Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Mux.Applicable}" IsReadOnly="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我的模板用于以下数据网格:
<DataGrid x:Name="pinTable" Grid.Row="2" ItemsSource="{Binding PortMux.Pins, Mode=TwoWay}" AutoGenerateColumns="False" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="Cell" Grid.ColumnSpan="28">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Selection" Binding="{Binding isSelected, Mode=TwoWay}" Width="Auto" Visibility="Hidden" />
<DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay}" Width="Auto" IsReadOnly="True"/>
<StaticResource ResourceKey="MyComboBoxFunctionColumn"/>
在相应的xaml类的cs文件后面的代码中我执行以下操作
public PortMux PortMux
{
get { return (PortMux)GetValue(PortMuxProperty); }
set { SetValue(PortMuxProperty, value); }
}
public static readonly DependencyProperty PortMuxProperty =
DependencyProperty.Register("PortMux", typeof(PortMux), typeof(MainWindow), new UIPropertyMetadata(null));...
public partial class MainWindow : Window
{...
PortMux = new PortMux();
...
PortMux = projectRef;
..
base.DataContext = this;
InitializeComponent();
...
使用新的集合类,但现在我的组合框项目是空的,但我不知道为什么?
<CollectionViewSource x:Key="src" Source="{Binding Path=Mux.Setting.FunctionList}" >
<DataGridTemplateColumn x:Key="MyComboBoxFunctionColumn" Header="Function">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource src}}" DisplayMemberPath="Name" SelectedIndex="{Binding Mux.Setting.Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Mux.Applicable}" IsReadOnly="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
enter code here
欢迎任何解决我问题的建议!!
提前谢谢!!