Silverlight DataGrid TemplateColumn 添加 ComboBox 的问题
我在这个问题上遇到了一点麻烦。 我制作了一个 DataGrid,它有 DataTemplate 列。 XAML
<data:DataGrid x:Name="PortsGrid" Height="124" Margin="23,0,17,2" VerticalAlignment="Bottom" ItemsSource="{Binding PortsGridData, Mode=TwoWay}" AutoGenerateColumns="False" SelectionMode="Single">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Specific port/Range" CanUserResize="False" Width="130" Binding="{Binding Port}"/>
<data:DataGridTemplateColumn Header="Timeout" CanUserResize="False" Width="130" >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="TimeoutComboBox" IsEditable="True" SelectedIndex="{Binding TimeoutIndex}">
<ComboBoxItem Content="First Value" Tag="-1"/>
<ComboBoxItem Content="Second Value" Tag="0"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Priority" CanUserResize="False" Width="120">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="PriorityComboBox" SelectedIndex="{Binding PriorityIndex}">
<ComboBoxItem Content="Low" Tag="0"/>
<ComboBoxItem Content="Prio1" Tag="1"/>
<ComboBoxItem Content="Prio2" Tag="2"/>
<ComboBoxItem Content="Prio3" Tag="3"/>
<ComboBoxItem Content="Prio4" Tag="4"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Compression" CanUserResize="False" Width="105">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="CompressionComboBox" SelectedIndex="{Binding CompressionIndex}">
<ComboBoxItem Content="On" Tag="1"/>
<ComboBoxItem Content="Off" Tag="0"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Protocol" CanUserResize="False" Width="100">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ProtocolComboBox" SelectedIndex="{Binding ProtocolIndex}">
<ComboBoxItem Content="Proto1"/>
<ComboBoxItem Content="Proto2"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
这是此数据网格与 ObservableCollection 绑定的 ,如果我通过在 ObservableCollection 中添加元素来在此数据网格中添加一行,组合框将添加其默认值,即未选择任何值。现在,如果我更改组合框选择,然后删除此行,然后再次添加此行,则组合框将添加其先前的状态,而不是默认状态。为什么会发生这种情况。可能是 DataGrid 正在存储组合框状态以获得更好的性能,但如何清除这些值?
I am in little trouble with this problem.
I have made a DataGrid and it have DataTemplate Columns. here is the XAML for that
<data:DataGrid x:Name="PortsGrid" Height="124" Margin="23,0,17,2" VerticalAlignment="Bottom" ItemsSource="{Binding PortsGridData, Mode=TwoWay}" AutoGenerateColumns="False" SelectionMode="Single">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Specific port/Range" CanUserResize="False" Width="130" Binding="{Binding Port}"/>
<data:DataGridTemplateColumn Header="Timeout" CanUserResize="False" Width="130" >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="TimeoutComboBox" IsEditable="True" SelectedIndex="{Binding TimeoutIndex}">
<ComboBoxItem Content="First Value" Tag="-1"/>
<ComboBoxItem Content="Second Value" Tag="0"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Priority" CanUserResize="False" Width="120">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="PriorityComboBox" SelectedIndex="{Binding PriorityIndex}">
<ComboBoxItem Content="Low" Tag="0"/>
<ComboBoxItem Content="Prio1" Tag="1"/>
<ComboBoxItem Content="Prio2" Tag="2"/>
<ComboBoxItem Content="Prio3" Tag="3"/>
<ComboBoxItem Content="Prio4" Tag="4"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Compression" CanUserResize="False" Width="105">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="CompressionComboBox" SelectedIndex="{Binding CompressionIndex}">
<ComboBoxItem Content="On" Tag="1"/>
<ComboBoxItem Content="Off" Tag="0"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Protocol" CanUserResize="False" Width="100">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ProtocolComboBox" SelectedIndex="{Binding ProtocolIndex}">
<ComboBoxItem Content="Proto1"/>
<ComboBoxItem Content="Proto2"/>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
this data grid is binded with a ObservableCollection
, If I add a row in this datagrid by adding an element in ObservableCollection comboboxes are added with their default value, i.e. no value selected. Now if I change the combo boxes selection and then remove this row, and add this row again, the comboboxes will be added with their previous states, not the default state.. why is this happening.. It might be that DataGrid is storing the Combobox states for better performance, but how to clear those values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论