wpf 随记
(1)<Window.Resources>
<local:Human x:Key="human" Child="ABC"/> //类
</Window.Resources>
(2)获得Silverlight对象宿主的方法:Silverlight对象当前宿主的URL是
string url= App.Current.Host.Source.ToString();
(3)
x:Code标记可以将原本呆在后置代码文件里的C#代码搬到XAML文件中。x:Code的内容必须使用<![CDATA[…]]>进行转义。
<Window x:Class="MyFirstWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Grid>
<StackPanel>
<Button Content="按钮" Click="Button_Click"/>
</StackPanel>
<x:Code>
<![CDATA[
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("x:Code hello!!!");
}
]]>
</x:Code>
</Grid>
</Window>
(4): 给image添加提示信息
<Image Source="hudie.jpg" Width="110" Height="110"
Tag="My Home" Canvas.Left="30" Canvas.Top="20"
MouseEnter="Image_BlurEffect_MouseEnter" MouseLeave="Image_BlurEffect_MouseLeave" Cursor="Hand">
<Image.ToolTip >
My Home
</Image.ToolTip>
(5):动画添加背景音乐
<MediaElement x:Name="audio1_mp3" HorizontalAlignment="Right" Margin="0,0,161,161" VerticalAlignment="Bottom" Width="0" Height="0" Stretch="Fill"/>
<Storyboard x:Key="Storyboard1">
<MediaTimeline BeginTime="00:00:00" RepeatBehavior="Forever" Source="audio1.mp3" Storyboard.TargetName="audio1_mp3" d:DesignTimeNaturalDuration="236.416"/>
</Storyboard>
(6):wpf 多值绑定
接下来看一下WPF中如何进行多值绑定
1.不需要转换的值
<Window x:Class="WpfApplicationSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <!--WPF 单值绑定--> <TextBlock Text="{Binding Title}"></TextBlock> <!--WPF 多值绑定,结合StringFormat--> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat=" {0}-{1}"> <Binding Path="Title"></Binding> <Binding Path="Time"></Binding> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid> </Window>
2转换
<Window x:Class="WpfApplicationSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:WpfApplicationSample"> <Window.Resources> <local:MultiValueConverterSample x:Key="cv"></local:MultiValueConverterSample> </Window.Resources> <Grid> <!--WPF 单值绑定--> <TextBlock Text="{Binding Title}"></TextBlock> <!--WPF 多值绑定,结合StringFormat--> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat=" {0}-{1}"> <Binding Path="Title"></Binding> <Binding Path="Time"></Binding> </MultiBinding> </TextBlock.Text> </TextBlock> <!--WPF 多值绑定,结合Converter--> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource cv}"> <Binding Path="Title"></Binding> <Binding Path="Time"></Binding> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid> </Window>
看起来很好理解,对吧?这是WPF中为我们默认就提供的功能,确实很方便。
但是,这个特性(多值绑定)却没有在Silverlight中实现。
(7):按钮圆角修改模板
<!--圆角button--> <Style TargetType="Button"> <Setter Property="FontSize" Value="15"/> <Setter Property="Margin" Value="4"/> <Setter Property="FontWeight" Value="Black"/> <Setter Property="Foreground" Value="WhiteSmoke"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Background" Value="#FFFF4300"/> <Setter Property="Width" Value="100"/> <Setter Property="Height" Value="35"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" CornerRadius="16" Name="PART_Background"> <Border.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="White" Offset="0.0" /> <GradientStop Color="#FFFF4300" Offset="0.5" /> <GradientStop Color="White" Offset="0.0" /> </LinearGradientBrush> </Border.Background> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
(8):
带超链接的TEXTBLOCK
1 <TextBlock> 2 <Hyperlink 3 Click="ForgetPassword"> 4 忘记密码 5 </Hyperlink> 6 </TextBlock>
(9):
后台打开网页
private void OpenWeb(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start("http://www.cnblogs.com/submarine"); }