Combobox display value in Silverlight

I have a ComboBox with CheckBoxes for items. When the user checks or deselects the checkboxes, I want the selected values ​​to appear in ContentPresenter, separated by a comma. Currently, I have redefined ContentPresenter:

<ContentPresenter x:Name="ContentPresenter"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    Margin="{TemplateBinding Padding}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    ContentTemplate="{StaticResource SelectedOperationsText}"/>

ContentPresenter is by default part of the ComboBox style. Any tips on how to implement this feature?

ComboBox ItemTemplate is implemented as follows:

<DataTemplate x:Key="ComboItemTemplate">
     <Grid HorizontalAlignment="Left">
         <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Text}"/>
     </Grid>
</DataTemplate>
+3
source share
3 answers

This solution is not ideal (for example, you can create your own control template for management, inherited from combobox), but it works.

  • Xaml

    <my:MyComboBox Width="180" ItemsSource="{Binding TestItems}" Text="{Binding SelectedItemsText}">
        <my:MyComboBox.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Left">
                    <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Text}"/>
                </Grid>
            </DataTemplate>
        </my:MyComboBox.ItemTemplate>
    </my:MyComboBox>
    
  • Hack combo box:

    public class MyComboBox : ComboBox
    {
    private ContentPresenter selectedContent;
    
    
    public MyComboBox()
    {
        this.DefaultStyleKey = typeof(ComboBox);
    }
    
    
    public override void OnApplyTemplate()
    {
        this.selectedContent = this.GetTemplateChild("ContentPresenter") as ContentPresenter;
        this.RefreshContent();
        base.OnApplyTemplate();
        this.SelectionChanged += (s, e) =>
            {
                //Cancel selection
                this.SelectedItem = null;
                this.RefreshContent();
            };
    }
    
    
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    
    
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(MyComboBox), 
        new PropertyMetadata(null, new PropertyChangedCallback((s,e)=>((MyComboBox)s).RefreshContent())));
    
    
    private void RefreshContent()
    {
        if (this.selectedContent != null)
        {
            var tb = (TextBlock)this.selectedContent.Content;
            tb.Text = this.Text;
        }
    }
    }
    
  • MainViewModel

    public class MainViewModel : INotifyPropertyChanged
    {
    public MainViewModel()
    {
        this.InitializeTestItems();
    }
    
    
    public void InitializeTestItems()
    {
        this.TestItems = new List<TestItemModel>{
                    new TestItemModel{IsChecked=true, Text="first"},
                    new TestItemModel{IsChecked=false, Text="second"},
                    new TestItemModel{IsChecked=false, Text="third"}};
        this.RefreshSelectedItemsText();
        foreach (var item in this.TestItems)
            item.CheckChanged += (s, e) => this.RefreshSelectedItemsText();
    }
    
    
    private void RefreshSelectedItemsText()
    {
        SelectedItemsText = string.Join(", ", this.TestItems.Where(ti => ti.IsChecked).Select(ti => ti.Text));
    }
    
    
    public List<TestItemModel> TestItems { get; set; }
    
    
    private string selectedItemsText;
    
    
    public string SelectedItemsText
    {
        get { return selectedItemsText; }
        set
        {
            selectedItemsText = value;
            OnPropertyChanged("SelectedItemsText");
        }
    }
    }
    

4.ItemViewModel

public class TestItemModel
{
    private bool isChecked;

    public bool IsChecked
    {
        get { return isChecked; }
        set 
        { 
            isChecked = value;
            if (CheckChanged != null)
                CheckChanged(this, null);
        }
    }

    public string Text { get; set; }

    public event EventHandler<EventArgs> CheckChanged;
}
+3

, "ContentPresenter".

combox, , , ( SO):

ComboBox, TextBlock. ItemTemplate ComboBox Checked UnChecked. Text TextBlock, . XAML:

<Grid Name="LayoutRoot">
    <ComboBox ItemsSource="{Binding Path=SitesList}" Name="CBsites" DropDownOpened="ComboBox_DropDownOpened" DropDownClosed="ComboBox_DropDownClosed">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Path=Location}" Checked="SiteCheckBox_Checked" Unchecked="SiteCheckBox_Unchecked" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <TextBlock Name="TXTselected" IsHitTestVisible="False" VerticalAlignment="Center" Margin="6,0,0,0" />
</Grid>

, TextBlock. , .

0

codeplex: codeplex, , , .. , - ...

I prefer not to need a boolean in related data, so I cited bindable SelectedItems

0
source

Source: https://habr.com/ru/post/1769126/


All Articles