Populating combobox in datagrid with tooltips WPF

Viewed 78

Currently I am trying to populate a combobox dropdown list with a custom data structure containing the items value and the associated tooltip to describe the item.

The population of the list and the tooltip both work properly whenever the list is populated, however whenever an item is selected the binding references the object instead of the Status value within the object.

I have tried changing the relativesource property of the combobox as I know why it is referencing the object instead of the value (as the children of the combobox are able to access the values directly), but I do not know how to fix the problem.

To put it quite simply, I am trying to populate my combobox's dropdown values from a list in mainwindow.xaml.cs and then get those values from the binding and rebind them to the values in ticketinfo.

The code I am using to test this is below.

MainWindow.xaml

<DataGrid x:Name="dgData" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Subject}" Header="Subject" Width="*"/>
                <DataGridTemplateColumn Header="Status" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Status}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox Height="22"
                                      IsEditable="True"
                                      ItemsSource="{StaticResource StatusList}"
                                      SelectedItem="{Binding Status}"
                                      IsTextSearchEnabled="True">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Status}" ToolTip="{Binding ToolTip}" />
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                                </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding RaisedBy,
                                        Mode=TwoWay,
                                        UpdateSourceTrigger=PropertyChanged}" Header="Raised By" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>

MainWindow.xaml.cs

public class TicketInfo
    {
        public string Subject { get; set; }
        public string Status { get; set; }
        public string RaisedBy { get; set; }
    }

    //Handle Dropdown boxes
    public class StatusList : List<StatusCombo>
    {
        public StatusList()
        {
            this.Add(new StatusCombo("Test", "Test 2"));
            this.Add(new StatusCombo("Check", "Check 2"));
            this.Add(new StatusCombo("Fail", "Fail 2"));
        }
    }
    public class StatusCombo
    {
        public string Status { get; set; }
        public string ToolTip { get; set; }
        public StatusCombo(string s, string t)
        {
            Status = s;
            ToolTip = t;
        }
    }

EDIT: To clarify the situation here, it is not as simple as just binding directly to the Status variable in TicketInfo. Whenever an implementation like that is used it displays the namespace.classname for StatusCombo.

1 Answers

To bind Status value of the selected combobox item to the Status value of TicketInfo you can use binding to SelectedValue with provided SelectedValuePath. So in your case the combobox can be defined as

<ComboBox Height="22"
          IsEditable="True"
          ItemsSource="{StaticResource StatusList}"
          SelectedValue="{Binding Status}" SelectedValuePath="Status"
          IsTextSearchEnabled="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Status}" ToolTip="{Binding ToolTip}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Related