微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverob中的Combobox显示值

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐