silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverob中的Combobox显示值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有ComboBox与checkBoxes的项目. 当用户选中或取消选中复选框时,我希望所选值显示在以逗号分隔的ContentPresenter中. 目前我已经覆盖了ContentPresenter: <ContentPresenter x:Name="ContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalCon
我有ComboBoxcheckBoxes的项目.
用户选中或取消选中复选框时,我希望所选值显示以逗号分隔的ContentPresenter中.
目前我已经覆盖了ContentPresenter:

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

认情况下,ContentPresenter是ComboBox样式的一部分.
有关如何实现功能的任何提示

ComboBox ItemTemplate实现如下:

<DataTemplate x:Key="ComboItemTemplate">
     <Grid HorizontalAlignment="Left">
         <checkBox Ischecked="{Binding IscheckeD}" Content="{Binding Text}"/>
     </Grid>
</DataTemplate>

解决方法

解决方案并不理想(例如,您可以为从组合框继承的控件创建自定义控件模板),但它可以正常工作.

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

>组合框的黑客:

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

大佬总结

以上是大佬教程为你收集整理的Silverob中的Combobox显示值全部内容,希望文章能够帮你解决Silverob中的Combobox显示值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。