problem with Custom ComboBox not raising notification changes during SelectedItem change

Mesh Ka 345 Reputation points
2024-02-11T12:31:17.0866667+00:00

Hi, in my wpf C# .net 8 application i have a custom Control composed of a Label and ComboBox, the problem i am experiencing is that the Custom Control's ComboBox doesn't raise the SelectedItem change in the ViewModel, then i decided to test it with a normal wpf ComboBox and the notification was raised successfully, What can be the problem? Help fix the problem.

Here is the repos of this sample and Here is the Database i Used.
customcombobox

The two ComboBoxes you see use the same bindings and codes.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,401 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,678 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,289 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
0 comments No comments
{count} votes

Accepted answer
  1. gekka 6,766 Reputation points MVP
    2024-02-11T15:24:28.9133333+00:00

    TemplateBinding can receive only oneway from template parent.

    <Style TargetType="{x:Type local:FloatingLabelComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:FloatingLabelComboBox}">
                    <StackPanel FlowDirection="LeftToRight">
                        <Label Content="{TemplateBinding LabelText}"
                            Foreground="{TemplateBinding LabelForeground}"
                            FontSize="{TemplateBinding LabelFontSize}"
                            VerticalAlignment="Stretch"
                            Margin="-5 0 0 0" 
                    />
    
                        <ComboBox x:Name="comboBox"
                                ItemsSource="{TemplateBinding ComboBoxItemsSource}"
                                SelectedItem="{Binding ComboBoxSelectedItem,RelativeSource={RelativeSource  Mode=TemplatedParent},Mode=TwoWay}"
                                SelectedValuePath="{TemplateBinding ComboBoxSelectedValuePath}"
                                SelectedValue="{Binding ComboBoxSelectedValue,RelativeSource={RelativeSource  Mode=TemplatedParent},Mode=TwoWay}"
                                DisplayMemberPath="{TemplateBinding ComboBoxDisplayMemberPath}"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Center"
                                Width="Auto"
                                Height="Auto"
                                materialdesign:HintAssist.IsFloating="False"
                                materialdesign:ColorZoneAssist.Mode="Dark"
                                materialdesign:HintAssist.HintOpacity="0.10"
                                materialdesign:HintAssist.FontFamily="Century Gothic"
                                Foreground="{TemplateBinding ComboBoxForeground}">
    
                        </ComboBox>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful