WPF ComboBox with CheckBoxes – Wrong header text binded

I’m trying to display a custom text on the header of a combobox, following the solution given here:

wpf-combobox-with-checkboxes-display-info-about-checked-items

I achieved to get a similar result as below:

The header text is managed in a method from the VM, concatenating all texts (GroupName) or, if all are selected as here, display “All groups”.

The code looks like the following (after deleting some other useless code for the example):

        <ComboBox Text="{Binding SelectedGroupNames, FallbackValue='[List of groups]'}"
                  IsEditable="True"
                  IsReadOnly="True"
                  ItemsSource="{Binding TestGroups}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" >
                        <CheckBox IsChecked="{Binding GroupEnabled}"
                                  Content="{Binding GroupName}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

And in the VM, the binded property is the following:

        private string selectedGroupNames = "All groups";
        public string SelectedGroupNames
        {
            get { return selectedGroupNames; }
            set { selectedGroupNames = value; OnPropertyChanged(nameof(SelectedGroupNames)); }
        }

For information, the code updating the SelectedGroupNames is the following (reduced for the example):

        private void Group_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(TestGroups.Count() == TestGroups.GetAllEnabled().Count()) // Everything is selected
            {
                SelectedGroupNames = "All groups";
            }
            else if(TestGroups.GetAllEnabled().Count() == 0) // No test group is selected
            {
                SelectedGroupNames = "No group selected!";
            }
            else
            {
                SelectedGroupNames = string.Join(", ", TestGroups.GetAllEnabled().Select(x => x.GroupName)); // Some groups are selected
            }
        }

My issue happens when we select/click outside of a checkbox or a text: e.g. on the 2 mm padding left to the checkbox, or just on the right of the text. The Text displayed is not anymore a concatenation of the GroupName (property of my object) but the object itself: “Namespace.class.object”.

The same as if I did “myObject.ToString()”.

How can I avoid this situation?

  • Block the user to click somewhere else?

  • make a check on the string property “SelectedGroupNames”?

I already tried to make a check in the VM to see if the value is a string, but it’s not working. It’s always a string because of ToString() I suppose.

I tried the solution from Sinatr without success:

  • Added in the view: SelectedItem=”{Binding SelectedTestGroup}”
  • and in the VM:
        public ITestGroup SelectedTestGroup
        {
            get { return selectedTestGroup; }
            set { selectedTestGroup = value; OnPropertyChanged(nameof(SelectedTestGroup)); OnPropertyChanged(nameof(SelectedGroupNames)); }
        }

Edit: following solution from Sinatr, I changed the binding mode to OneWay (previously empty) and used CurrentDispatcher to raise property changed:

        public ITestGroup SelectedTestGroup
        {
            get { return selectedTestGroup; }
            set { selectedTestGroup = value; Dispatcher.CurrentDispatcher.InvokeAsync(() => OnPropertyChanged(nameof(SelectedGroupNames)));  }
        }

And it works! Great

8

As suggested by Sinatr, one workaround was to use a fake property to update the text from the ComboBox, with the Dispatcher to produce a small delay:

    public ITestGroup SelectedTestGroup
    {
        get { return selectedTestGroup; }
        set { selectedTestGroup = value; Dispatcher.CurrentDispatcher.InvokeAsync(() => OnPropertyChanged(nameof(SelectedGroupNames)));  }
    }

And secondly change the binding mode to OneWay:

Text="{Binding SelectedGroupNames, FallbackValue='[List of groups]', Mode=OneWay}"

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