我有一個組合框:
<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 欄位以及相應的屬性):設定 Combobox 的選定值而不觸發 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 ComboBox SelectedItem - change to previous value,但我很不確定如何在我的概念驗證中實作它。我的 SourceIndex 設定器中有提到的設定器嗎?如果是這樣,在我的情況下 CancelChange() 在哪里?
對于這兩個問題,我將不勝感激。
uj5u.com熱心網友回復:
為了防止默認值,我通常使用一個簡單的布爾變數(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
{
}
}
}
uj5u.com熱心網友回復:
其次,當用戶在 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;
}
}
uj5u.com熱心網友回復:
如果我正確理解了您要實作的內容:
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527041.html
標籤:C#wpf
