初学.net ,某日傍晚纠结于吃什么。于是决定开始弄一个基于WPF的【吃什么】。
以下指示记录自己学到东西。
1.List容器
a.
List<T> Name = new List<T>();
b.
Name.Add(item);
Name.insert(loc, item);//loc 指下标
Name.remove(item);
Name.removeAt(loc);
Name.Count();
Name.Clear();//清空所有项
2.线程
using System.Threading;
Thread t= new Thread(func);
t.Start();
Thread.Sleep(10);//静态方法
t.Abort();
3.关于从控件动态创建,到事件注册
//以在ListBox里添加一个ListBoxItem为例,
//并为其创建右键菜单(ContextMenu),
//在菜单里添加一个【删除】选项,并注册事件
ListBoxItem lbi = new ListBoxItem();
lbi.Content = "新建项";
ContextMenu cm = new System.Windows.Controls.ContextMenu();
lbi.ContextMenu = cm;
MenuItem del = new MenuItem();
del.Header = "删除";
del.Click += new RoutedEventHandler(myHandler);
cm.Items.Add(del);
listBox.Items.Add(lbi);
private void myHandler(object sender, RoutedEventArgs e)
{
ListBoxItem buf = new ListBoxItem();
buf = listbox.SelectedItem as ListBoxItem;
listbox.Items.Remove(buf);
}
关于事件,sender代表事件的发生者,例如上面的myHandle中,sender代表del,而e代表事件本身。
click本身没什么可用,在鼠标移动事件中,可以通过e得到指针坐标。
下面记录一个在文本框(TextBox)里,按回车键的事件注册。
//文本框用xaml产生
//<TextBox Name="textbox_new" KeyDown="textbox_new_KeyDown" />
//当接受回车键时,做出相应
private void textbox_new_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//响应
}
}
4.一些常用的鼠标事件
1)Click。
2)MouseEnter和MouseLeave
考虑到一个问题:当鼠标移动到某个控件(比如button)上时,对其修改(比如修改button上的Content),鼠标移开后继续对其修改(或者复原)。
这里用到了两个事件:MouseEnter和MouseLeave(顾名思义)。
5.下拉选择框
<ComboBox Name="groupBox" Text="1" Margin="32,123,0,0" VerticalAlignment="Top" Width="52" RenderTransformOrigin="1.326,0.958"
SelectionChanged="ComboBox_SelectionChanged" BorderBrush="#FF707070"
SnapsToDevicePixels="True" Foreground="Black" Focusable="False" HorizontalAlignment="Left" SelectedIndex="0" >
<ComboBoxItem Content="1" Name="comboBox_I1" Width="52" Height="21"/>
<ComboBoxItem Content="2" Name="comboBox_I2" Width="52" Height="21"/>
<ComboBoxItem Content="3" Name="comboBox_I3" Width="52" Height="21"/>
<ComboBoxItem Content="4" Name="comboBox_I4" Width="52" Height="21"/>
<ComboBoxItem Content="5" Name="comboBox_I5" Width="52" Height="21"/>
</ComboBox>
6.WPF在线程管理上要求只能由UI线程控制界面。因此在开其他线程控制某个控件时需要进行如下操作
Dispatcher.Invoke((Action)delegate //只能在ui线程做ui
{
label.Content = itemList[i].Content;
});
7.关于在后台为动态添加的控件设置颜色
ListBoxItem l = new ListBoxItem();
//设置一些属性
l.Height = 13;
l.FontSize = 8;
//设置content的位置
l.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
l.BorderThickness = new Thickness(0.7, 0.7, 0.7, 0.7);
//设置颜色
Color c = (Color)ColorConverter.ConvertFromString("#FFABADB3");
itemList[itemList.Count - 1].BorderBrush = new SolidColorBrush(c);
8.添加并使用样式
这里写了2中按钮的样式
<Window.Resources> <!--这里用的WPF,在winStore里应该是<Page.Resources> -->
<Style TargetType="{x:Type Button}" x:Key="btSqStyle">
<Setter Property="Background" Value="Black"></Setter>
<Setter Property="BorderBrush" Value="#FF787878"></Setter>
<Setter Property="BorderThickness" Value="0.6"></Setter>
<Setter Property="Width" Value="37" ></Setter>
<Setter Property="Height" Value="42"></Setter>
</Style>
<Style TargetType="{x:Type Button}" x:Key="btCommonStyle">
<Setter Property="Background" Value="#FFCBCBCB"></Setter>
<Setter Property="BorderBrush" Value="#FF595959"></Setter>
<Setter Property="BorderThickness" Value="0.7"></Setter>
</Style>
</Window.Resources>
<Button Content="" HorizontalAlignment="Left" Margin="71,0,0,0" VerticalAlignment="Top" Style="{StaticResource btSqStyle}" Width="36"/>
<Button Name="bt_clear" Content="清空" Margin="84,123,358,175" Click="bt_clear_Click" Style="{StaticResource btCommonStyle}"
HorizontalContentAlignment="Center" FontSize="10" ForceCursor="True" VerticalContentAlignment="Center" />