Silverob中的Combobox显示值

Silverob中的Combobox显示值,第1张

概述我有ComboBox与CheckBoxes的项目. 当用户选中或取消选中复选框时,我希望所选值显示在以逗号分隔的ContentPresenter中. 目前我已经覆盖了ContentPresenter: <ContentPresenter x:Name="ContentPresenter" HorizontalAlignment="{TemplateBinding HorizontalCon 我有ComboBox与CheckBoxes的项目.
当用户选中或取消选中复选框时,我希望所选值显示在以逗号分隔的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显示值所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078862.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存