新项目

时间:2016-03-15 12:19:01

标签: c# wpf combobox

我有2个组合框,它的项目源绑定到列表视图中的2个列表。这一切都很好但是当我在列表中添加一个新的新项目时,组合框的值是空白的,我希望它们能够显示我的所有区域#34;一个和所有相"在另一个。我如何让它工作?我尝试了很多例子,但都希望我使用" IsSynchronizedWithCurrentItem"为true或者SelectedIndex为0但是这也将当前的组合框值设置为SelectedIndex,这不是我想要的,如果组合框是空的,我只想要它。有人可以帮帮我吗?

ListView中的Combobox

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

添加新规则方法

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

可观察收集(编辑)

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();
        }
    }

1 个答案:

答案 0 :(得分:1)

不使用私有变量规则,而是在按钮单击事件中使用属性规则。

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