大佬教程收集整理的这篇文章主要介绍了Silverob中的Combobox显示值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<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,请注明来意。