字符串比较
在String.IndexOf Method (System) - MSDN - Microsoft中提到:
As explained in Best Practices for Using Strings in the .NET Framework, we recommend that you avoid calling string comparison methods that substitute default values and instead call methods that require parameters to be explicitly specified.
也就是说,应该使用明确的IndexOf(str, StringComparison.Ordinal)
这样的方式。
Using 'switch' with strings in resource file
或者using collection of strings in a switch statement,How to assign the ResourceDictionary string as switch case Constant Expression?
You can't do that. The compiler must be able to evaluate the values, which means that they need to be literals or constants.
- 方法1:使用Action映射
You could use a Dictionary<string, Action>. You put an Action (a delegate to a method) for each string in the Dictionary and search it.
var actions = new Dictionary<string, Action> {
{ "String1", () => Method1() },
{ "String2", () => Method2() },
{ "String3", () => Method3() },
};
Action action;
if (actions.TryGetValue(myString, out action))
{
action();
}
else
{
// no action found
}
As a sidenote, if Method1 is already an Action or a void Method1() method (with no parameters and no return value), you could do
{ "String1", (Action)Method1 },
TabControl的标签页切换
- 方法1:You can use SelectionChanged event in TabControl and use switch case to do anything you like.
XAML Code
<TabControl SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Item1"></TabItem>
<TabItem Header="Item2"></TabItem>
<TabItem Header="Item3"></TabItem>
</TabControl>
Behind Code
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string tabItem = ((sender as TabControl).SelectedItem as TabItem).Header as string;
switch(tabItem)
{
case "Item1":
break;
case "Item2":
break;
case "Item3":
break;
default:
return;
}
}
- 方法2:
You can do this by adding labels to the header property for each tabitem in the tabcontrol. Then you can set an event for the label.
xaml
<TabControl Height="100" HorizontalAlignment="Left" Name="tabControl1">
<TabItem Name="tabItem1">
<TabItem.Header>
<Label Content="tabItem1" MouseLeftButtonDown="tabItem1_Clicked" HorizontalAlignment="Stretch"/>
</TabItem.Header>
<Grid />
</TabItem>
<TabItem Name="tabItem2">
<TabItem.Header>
<Label Content="tabItem2" MouseLeftButtonDown="tabItem2_Clicked" HorizontalAlignment="Stretch"/>
</TabItem.Header>
<Grid />
</TabItem>
</TabControl>
C# / Code Behind
private void tabItem1_Clicked(object sender, MouseButtonEventArgs e)
{
//DO SOMETHING
}
private void tabItem2_Clicked(object sender, MouseButtonEventArgs e)
{
//DO SOMETHING
}
合理使用ObservableCollection和BindingList
-
StackOverflow上的回答之二
The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF.
From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.
-
StackOverflow上的回答之一
An ObservableCollection can be updated from UI exactly like any collection. The true difference is rather straightforward:
ObservableCollection<T> implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated.
However, BindingList<T> implements IBindingList.
IBindingList provides notification on collection changes, but not only that. It provides a whole bunch of functionality which can be used by the UI to provide a lot more things than only UI updates according to changes, like:
Sorting
Searching
Add through factory (AddNew member function).
Readonly list (CanEdit property)
All these functionalities are not available in ObservableCollection<T>
Another difference is that BindingList relays item change notifications when its items implement INotifyPropertyChanged. If an item raises a PropertyChanged event, the BindingList will receive it an raises a ListChangedEvent with ListChangedType.ItemChanged and OldIndex=NewIndex (if an item was replaced, OldIndex=-1). ObservableCollection doesn't relay item notifications.
Note that in Silverlight, BindingList is not available as an option: You can however use ObservableCollections and ICollectionView (and IPagedCollectionView if I remember well).