1. 程式人生 > >DataGrid中DataGridComboBoxColumn列SelectedValueBinding注意Mode設定

DataGrid中DataGridComboBoxColumn列SelectedValueBinding注意Mode設定

今天在使用DataGrid的下拉列表列時因設定SelectedValueBinding="{Binding 觸發沿, Mode=OneWay}",導致下拉列表一直無法顯示,查詢半天最後發現將Mode改成TwoWay,問題解決。

<DataGrid VerticalAlignment="Stretch" x:Name="dgItems"
      Height="Auto"
      SelectionUnit="FullRow" SelectionMode="Extended" CanUserAddRows="False"
      DataContext="{Binding}"
Width="Auto" AutoGenerateColumns="False" FontSize="16" AlternatingRowBackground="LightBlue" AlternationCount="2" Margin="10, 10" Background="Transparent" ScrollViewer.VerticalScrollBarVisibility="Visible">
<DataGrid.Resources> <col:ArrayList x:Key="key_triggerList"
>
<col:DictionaryEntry Key="上升沿" Value="上升沿"/> <col:DictionaryEntry Key="下降沿" Value="下降沿"/> </col:ArrayList> <col:ArrayList x:Key="key_attenuatorList"> <col:DictionaryEntry Key=" 0dB" Value="0"/> <col:DictionaryEntry
Key="20dB" Value="20"/>
<col:DictionaryEntry Key="40dB" Value="40"/> <col:DictionaryEntry Key="60dB" Value="60"/> </col:ArrayList> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="IP" Width="120*" Binding="{Binding IP, Mode=OneWay}" /> <DataGridComboBoxColumn Header="觸發沿" Width="80*" IsReadOnly="False" DisplayMemberPath="Key" SelectedValuePath="Value" SelectedValueBinding="{Binding 觸發沿, Mode=TwoWay}" ItemsSource="{StaticResource key_triggerList}"/> <DataGridTextColumn Header="觸發電平(V)" IsReadOnly="False" Width="80*" Binding="{Binding 觸發電平, Mode=TwoWay}" /> <DataGridComboBoxColumn Header="衰減器" IsReadOnly="False" Width="80*" DisplayMemberPath="Key" SelectedValuePath="Value" SelectedValueBinding="{Binding 衰減器, Mode=TwoWay}" ItemsSource="{StaticResource key_attenuatorList}"/> </DataGrid.Columns> </DataGrid>

注意在xaml的<window/>節增加

<window xmlns:col="clr-namespace:System.Collections;assembly=mscorlib">

</window>