質問

Been looking around for about 4 hours so if the answer is here somewhere i'm sorry. Anyway.. here goes:

I have a DataGrid and am trying to implement a DatGridComboBoxColumn. I have gotten as far as managing to get it to come up with correct data. However, when i make a selection it changes not only the column selected but also another column in the master table.

Specifically:

my Groups table looks like this:
Id,Name
1,A
2,B
3,C

If I select A in the DataGridComboBoxColumn it changes the GroupId field in the Schedules DataSource to the right value, 1. But - it also changes the Name field in the Schedules DataSource to "A". I have tried about 70 different combinations of how to set things. The only thing that has worked in to rename the Name field in Schedules to something else like ScheduleName. Is this some kind of bug in WPF or am i not coding something right?

Any help will be much appreciated!
Orson

Here is the Page code:

<Page x:Class="Bix.SchedulesMainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="565" d:DesignWidth="775"
    Title="SchedulesMainPage" xmlns:my="clr-namespace:Bix">
    <Page.Resources>
        <ObjectDataProvider x:Key="ScheduleDataProvider" ObjectType="{x:Type my:ScheduleDataProvider}"/>
        <ObjectDataProvider x:Key="Schedules"
          ObjectInstance="{StaticResource ScheduleDataProvider}"
          MethodName="GetSchedules"/>
        <ObjectDataProvider x:Key="GroupDataProvider" ObjectType="{x:Type my:GroupDataProvider}"/>
        <ObjectDataProvider x:Key="Groups"
          ObjectInstance="{StaticResource GroupDataProvider}"
          MethodName="GetGroups"/>
        <ObjectDataProvider x:Key="ReportDataProvider" ObjectType="{x:Type my:ReportDataProvider}"/>
        <ObjectDataProvider x:Key="Reports"
          ObjectInstance="{StaticResource ReportDataProvider}"
          MethodName="GetReports"/>
    </Page.Resources>
    <Grid>
        <Label Content="Schedules" FontSize="32" FontWeight="Bold" Foreground="#FF5A5A5A" Height="52" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" Width="719" />
        <DataGrid AutoGenerateColumns="False" DataContext="{Binding Source={StaticResource Schedules}}" Height="224" HorizontalAlignment="Left" HorizontalGridLinesBrush="#FFCBCBCB" ItemsSource="{Binding}" Margin="12,68,0,0" Name="dgrdContacts" VerticalAlignment="Top" VerticalGridLinesBrush="#FFCBCBCB" Width="751">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="120" />
                <DataGridComboBoxColumn Header="Contact Group"
                    SelectedValueBinding="{Binding Path=GroupId}" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource Groups}}">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding Source={StaticResource Groups}}" />
                            <Setter Property="Text" Value="{Binding Name}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding Source={StaticResource Groups}}" />
                            <Setter Property="Text" Value="{Binding Name}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <DataGridTextColumn Binding="{Binding Path=Interval}" Header="Send Every" Width="120" />
                <DataGridTextColumn Binding="{Binding Path=IntervalUnit}" Header="Time Unit" Width="120" />
                <DataGridTextColumn Binding="{Binding Path=LastSend}" Header="Last Sent" Width="*" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Page>
役に立ちましたか?

解決

You're binding the Text property of the ComboBox to the Name property on the underlying row in the data source. Looking at the code, I don't see any need for the EditingElementStyle and ElementStyle styles, since you're already setting the relevant properties outside of it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top