0
我有2個組合框,它的itemssource綁定到列表視圖中的2個列表。這一切都可以正常工作,但是當我向列表中添加一個新的項目時,組合框值爲空,我希望它們向我展示一個「所有區域」和另一箇中的「所有區域」。我如何得到這個工作?我已經嘗試了很多例子,但都希望我使用「IsSynchronizedWithCurrentItem」爲true或SelectedIndex爲0,但是這也將當前組合框的值設置爲SelectedIndex,這不是我想要的,只有當組合框爲空時才需要。誰能幫幫我嗎?新項目的組合框默認值
組合框的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>
添加新的規則方法
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();
}
}
感謝這個工作完美:) –
只是一個快速好奇的問題,是否有任何可能的方式來處理數據做這將觸發所有或任何類似?或者點嘿,只是替換你現有的值,並忽略任何當前的存儲值? –