如何在加载组合框的值时不引发消息"default"?

  • 本文关键字:消息 default 加载 组合 c# wpf
  • 更新时间 :
  • 英文 :


我有一个组合框:

<ComboBox Height="25" Width="150"
Name="EnvironmentComboBox"
ItemsSource="{Binding Path=SourceList}"
SelectedIndex="{Binding Path=SourceIndex}"
SelectionChanged="EnvironmentComboBox_SelectionChanged">
</ComboBox>

在后面的代码中,我填充SourceList:

public MainWindow()
{
InitializeComponent();
ConfigurationService.SetEnvironmentValues(ConfigurationService.DefaultEnvironment);

DataContext = this;
//SourceIndex = 0;
List<ComboBoxItem> source = new List<ComboBoxItem>
{
//new ComboBoxItem  { Content = " - Select Environment - "},
new ComboBoxItem  { Content = "PROD"},
new ComboBoxItem  { Content = "CERT"},
new ComboBoxItem  { Content = "DEV"},
};
SourceList = source;
}

这在很大程度上是基于我在这里发现的内容(包括_sourceIndex和_sourceList字段以及相应的属性(:设置组合框';s未触发SelectionChanged事件的选定值

我有一个SelectionChanged事件,它在ComboBox选择更改后触发:

private void EnvironmentComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(SourceIndex == 0))
{
String env = ((ComboBoxItem)((ComboBox)sender).SelectedValue).Content.ToString();
string message = $"Are you sure you want to change environment to {env}?nAll unsaved work will be lost!";
const string caption = "Change Environment?";
MessageBoxResult userResponse = MessageBox.Show(message, caption,
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (userResponse == MessageBoxResult.Yes)
{
bool envChange = ConfigurationService.SetEnvironmentValues(env);
EnvironmentChangedMessage(envChange);
}
else
{
}
}
}

这里确实有两个问题。

首先,SelectionChanged事件似乎在应用程序启动时运行,我认为进行数据绑定会解决这个问题(但事实并非如此(。所以我想,我会加一个"-选择环境-";ComboBoxItem(你可以看到它被注释掉了(,然后有那个条件!(SourceIndex==0(,以防止在我的ConfigurationService类中切换环境的代码;伪";值。然而,我真的只是希望PROD加载在ConfigurationService类中,并且在应用程序启动时,它也是选定的索引。因此,我不得不在应用程序启动之前获取MessageBox,或者PROD不更改,因为它等于索引0。

第二,当用户点击";否";在MessageBox上,我想将所选组合框项目的值恢复为原来的值。我回顾了这一点:WPF组合框SelectedItem-更改为以前的值,但我不确定如何在概念验证中实现这一点。我的SourceIndex setter中有提到的setter吗?如果是,那么在我的情况下CancelChange((在哪里?

如果能在这两个问题上得到任何帮助,我将不胜感激。

为了防止默认值,我通常使用一个简单的布尔变量(Is_Loaded(,如下

bool Is_Loaded=false;
public MainWindow()
{
InitializeComponent();
ConfigurationService.SetEnvironmentValues(ConfigurationService.DefaultEnvironment);

DataContext = this;
//SourceIndex = 0;
List<ComboBoxItem> source = new List<ComboBoxItem>
{
//new ComboBoxItem  { Content = " - Select Environment - "},
new ComboBoxItem  { Content = "PROD"},
new ComboBoxItem  { Content = "CERT"},
new ComboBoxItem  { Content = "DEV"},
};
SourceList = source;
//---------------------
Is_Loaded=true;
}

private void EnvironmentComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!Is_Loaded){ return;}
//================
if (!(SourceIndex == 0))
{
String env = ((ComboBoxItem)((ComboBox)sender).SelectedValue).Content.ToString();
string message = $"Are you sure you want to change environment to {env}?nAll unsaved work will be lost!";
const string caption = "Change Environment?";
MessageBoxResult userResponse = MessageBox.Show(message, caption,
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (userResponse == MessageBoxResult.Yes)
{
bool envChange = ConfigurationService.SetEnvironmentValues(env);
EnvironmentChangedMessage(envChange);
}
else
{
}
}
}

如果我正确理解了您想要实现的内容:

public partial class MainWindow : Window
{
public IReadOnlyList<string> SourceList { get; }
= Array.AsReadOnly(new string[] { "PROD", "CERT", "DEV" });
public MainWindow()
{
InitializeComponent();
}
private object oldSelectedItem = null;
private const string message = "Are you sure you want to change environment to {0}?nAll unsaved work will be lost!";
private const string caption = "Change Environment?";
private void EnvironmentComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selector = (Selector)sender;
if (oldSelectedItem is null)
{
oldSelectedItem = selector.SelectedItem;
return;
}
if (oldSelectedItem == selector.SelectedItem)
{
return;
}
string env = (string)selector.SelectedItem;
MessageBoxResult userResponse = MessageBox.Show(string.Format(message, env), caption,
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (userResponse == MessageBoxResult.Yes)
{
bool envChange = ConfigurationService.SetEnvironmentValues(env);
EnvironmentChangedMessage(envChange);
oldSelectedItem = selector.SelectedItem;
}
else
{
selector.SelectedItem = oldSelectedItem;
}
}
<ComboBox SelectedIndex="0">
<FrameworkElement.Resources>
<CollectionViewSource x:Key="sourceList"
Source="{Binding SourceList}"/>
</FrameworkElement.Resources>
<ItemsControl.ItemsSource>
<CompositeCollection>
<ComboBoxItem Visibility="Collapsed">
- Select Environment -
</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource sourceList}}"/>
</CompositeCollection>
</ItemsControl.ItemsSource>
</ComboBox>

第二,当用户点击"否";在MessageBox上,我想还原所选组合框项目的值与其原始值的比值。

为什么不使用变量来保存当前值,并在需要时将其检索回来?

int My_ComboBox_Previous_SelectedIndex = -1;
private void My_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (My_ComboBox.SelectedIndex == My_ComboBox_Previous_SelectedIndex)
{
return;
}
if (MessageBox.Show("Are You Sure", "Confirm ?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
My_ComboBox.SelectedIndex = My_ComboBox_Previous_SelectedIndex;
return;
}
else
{
My_ComboBox_Previous_SelectedIndex = My_ComboBox.SelectedIndex;
}
}

相关内容

最新更新