Silverlight中的组合框显示值 [英] Combobox display value in Silverlight

查看:124
本文介绍了Silverlight中的组合框显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ComboBox与复选框的项目。
当用户选中或取消选中复选框时,我想要将选定的值显示在ContentPresenter中,并以逗号分隔。
目前我已覆盖ContentPresenter:

I have ComboBox with CheckBoxes for items. When user checks or uncheckes boxes I want the selected values to be displayed in the ContentPresenter separated by comma. At the the moment I have overridden ContentPresenter:

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

ContentPresenter默认为ComboBox样式的一部分。
如何实现此功能的任何提示?

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

ComboBox ItemTemplate实现如下:

ComboBox ItemTemplate is implemented like this:

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


推荐答案

这个解决方案并不理想可以为继承自combobox的控件创建自定义控件模板)。

This solution isn't ideal (for example, you can create custom control template for control inherited from combobox), but it works.


  1. Xaml

  1. 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 of the combobox:

    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



  • 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");
        }
    }
    }
    



  • b $ b

    4.ItemViewModel

    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;
    }
    

    这篇关于Silverlight中的组合框显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆