Make ComboBox Border red if item is not yet selected

I’m new to C# and WPFs, and honestly just started learning on my own this week. I’m trying to make a drop down menu that highlights red (signaling that this field is required) until an item is selected. I don’t have an item on the list that’s empty per se, but the default state is a blank option. Once one item is selected, you cannot reach the blank option again.

I’ve tried a bunch of different approaches, and believe that I am at the understanding that I need to make a style template that requests validation in order to change the border color. However, I’m encountering some issues with that. It never really sees a validation error when running. I think I must just have a fundamental misunderstanding of how to use validations (basically the syntax requirements of making my own validation and using it for this trigger).

If there is any way to accomplish this without validations that might be easier for me, but I didn’t see anything online describing that. Any help would be appreciated.

My XAML ComboBox template style:

 <Style x:Key="ComboBoxValidationStyle" TargetType="ComboBox">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="ComboBox">
                 <Border x:Name="border" BorderBrush="Green" BorderThickness="1">
                     <ComboBox x:Name="PART_EditableTextBox"
                               ItemsSource="{TemplateBinding ItemsSource}" 
                               SelectedItem="{Binding SelectedModelNumber, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">
                     </ComboBox>
                 </Border>
                 <ControlTemplate.Triggers>
                     <Trigger Property="Validation.HasError" Value="True">
                         <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                         <Setter Property="BorderBrush" TargetName="border" Value="Red"/>
                         <Setter Property="BorderThickness" TargetName="border" Value="1"/>
                     </Trigger>
                     <Trigger Property="IsMouseOver" Value="True">
                         <Setter Property="BorderBrush" TargetName="border" Value="#00aed7"/>
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>

MY XAML ComboBox Code:

           <ComboBox Grid.Row="1" FontSize="15" Grid.Column="2" Width="110" Height="27" Margin="26,0,0,0" Style="{StaticResource ComboBoxValidationStyle}" ItemsSource="{Binding ModelNumbers}" Name="comboBox1">
               <ComboBox.SelectedItem>
                   <Binding Path="Name" Mode="TwoWay" RelativeSource="{RelativeSource Mode=TemplatedParent}" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                       <Binding.ValidationRules>
                           <local:SelectionValidationRule></local:SelectionValidationRule>
                       </Binding.ValidationRules>
                   </Binding>
               </ComboBox.SelectedItem>
           </ComboBox>

My Validation code:

    public class SelectionValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value is string selectedItemString)
            {
                if (string.IsNullOrEmpty(selectedItemString))
                {
                    return new ValidationResult(false, null);
                }
            }
            else if (value == null || string.IsNullOrEmpty(value.ToString()))
            {
                return new ValidationResult(false, null);
            }
            else if (value is ComboBox comboBox)
            {
                if (comboBox.SelectedIndex == -1)
                {
                    return new ValidationResult(false, null);
                }
            }

            return new ValidationResult(true, null);
        }
    }

And yes, I did include the SelectionValidationRule class in my XAML code behind!

New contributor

anon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật