0

嗨,我有一个 wpf 项目,它将数据网格绑定到这样的模块:

<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
        <DataGridTemplateColumn Header="Values">
           <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                <ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
              </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

这工作正常。现在我想添加一些功能,包括将事件设置为组合框SelectionChanged。所以我改变了xaml这样的文件:

<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
        <DataGridTemplateColumn Header="Values">
           <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                <ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    /!-- Add an EventSetter here. -->
                    <Style TargetType="{x:Type ComboBox}">                                                                                           
                         <EventSetter Event="SelectionChanged" Handler="ValueChanged" />                                                                                  
                    </Style>
                </ComboBox>
              </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

并在后端添加一个函数,如下所示:

    private void ValueChanged(object sender, SelectionChangedEventArgs e)
    {
         var comboBox = sender as ComboBox;
         if (comboBox.SelectedItem != null)
         {
              //do something here.
         }
    }

error: "The Application is in break mode."但是,当我更改组合框单元格时,这会使项目崩溃。那么可能是什么问题呢?提前致谢!

4

2 回答 2

0

像我一样愚蠢。只需在combobox修复问题中设置事件。

<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"/>
    <DataGridTemplateColumn Header="Values">
       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                     SelectionChanged="ValueChanged" />
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

于 2018-05-29T08:49:21.750 回答
0

您需要将 的Style属性设置ComboBox为您的Style

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ComboBox.Style>
                <Style TargetType="{x:Type ComboBox}">
                    <EventSetter Event="SelectionChanged" Handler="ValueChanged" />
                </Style>
            </ComboBox.Style>
        </ComboBox>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

或者您可以直接在ComboBox元素上连接事件处理程序并摆脱Style

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  SelectionChanged="ValueChanged" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
于 2018-05-29T12:31:24.287 回答