简体   繁体   English

新项目的组合框默认值

[英]Combobox default value for new item

I have 2 comboboxs that has it's itemssource bound to 2 lists which are in a listview. 我有2个组合框,它的itemssource绑定到listview中的2个列表。 This all works fine however when I add a new new item to the list the combobox values are blank and I want them to show me "All Zones" for one and "All Facies" in the other. 一切正常,但是当我向列表中添加新的新项目时,组合框值为空白,我希望它们向我显示一个的“所有区域”和另一个的“所有相”。 How do I get this to work? 我该如何工作? I have tried many examples but all want me to use the "IsSynchronizedWithCurrentItem" to be true or the SelectedIndex to 0 however this also sets the current combobox values to the SelectedIndex which is not what I want I only want it if the combobox is blank. 我已经尝试了许多示例,但是所有人都希望我使用“ IsSynchronizedWithCurrentItem”为true或SelectedIndex设置为0,但这也将当前组合框值设置为SelectedIndex,这不是我想要的,如果组合框为空,我只想要它。 Can anyone please help me? 谁能帮帮我吗?

Combobox's inside the ListView 组合框位于ListView内

<GridViewColumn Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox Style="{DynamicResource ComboBoxStyle1}" x:Name="zoneComboBox" Margin="0,0,5,0" Height="20" Width="80" ItemsSource="{Binding DataContext.Zones, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, Mode=FindAncestor}}" SelectedValue="{Binding Zone}" SelectedIndex="0"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                            <GridViewColumnHeader>
                                <TextBlock Text="Zones" FontFamily="{DynamicResource FontFamily}" FontSize="11" FontWeight="Bold"/>
                            </GridViewColumnHeader>
                        </GridViewColumn>
                        <GridViewColumn Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox Style="{DynamicResource ComboBoxStyle1}" x:Name="faciesComboBox" Margin="0,0,5,0" Height="20" Width="80" ItemsSource="{Binding DataContext.Facies, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, Mode=FindAncestor}}" SelectedValue="{Binding Facie}" SelectedIndex="0"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                            <GridViewColumnHeader>
                                <TextBlock Text="Facies" FontFamily="{DynamicResource FontFamily}" FontSize="11" FontWeight="Bold"/>
                            </GridViewColumnHeader>
                        </GridViewColumn>

Add new rule method 添加新的规则方法

private void AddRuleBtn_Click(object sender, RoutedEventArgs e)
    {
        rules.Add(new GeologicalAnalysisRule());
    }

Observable Collection (Edit) 可观察的集合(编辑)

private ObservableCollection<GeologicalAnalysisRule> rules;

    public RuleSetterControl()
    {
        InitializeComponent();
        Rules = new ObservableCollection<GeologicalAnalysisRule>();
        Rules.Add(new GeologicalAnalysisRule());
    }

    public ObservableCollection<GeologicalAnalysisRule> Rules
    {
        get { return rules; }
        set
        {
            if (Equals(value, rules)) return;
            rules = value;
            OnPropertyChanged();
        }
    }

Instead of using private variable rules use Property Rules in the button click event. 可以使用按钮单击事件中的“属性规则”来代替使用私有变量规则。

private void AddRuleBtn_Click(object sender, RoutedEventArgs e)
{
    Rules.Add(new GeologicalAnalysisRule());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM