WPF学习

元素绑定

 <ListBox Name="listbox1" Margin="3" Height="auto">
 <ListBoxItem Tag="Red">Red</ListBoxItem>
 <ListBoxItem Tag="Blue">Blue</ListBoxItem>
 <ListBoxItem Tag="Pink">Pink</ListBoxItem>
 </ListBox>
<TextBlock Text="simple" Name="textblock" TextWrapping="Wrap" Background="{Binding Path=SelectedItem.Tag,Mode=OneWay,ElementName=listbox1}" Margin="10" />
//path绑定元素的路径,elementName  绑定对象的name,mode 绑定模式 单向 双向等....
   private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Binding binding = new Binding();
            binding.Source = slider1;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.textblock.SetBinding(TextBlock.FontSizeProperty, binding);
        }
//可以手动以代码的方式绑定 推荐在xml文件中以命令的形式进行绑定

绑定到非元素 Source、RelativeSource、DataContext

//Source绑定
<Window.Resources>
        <FontFamily x:Key="CustomFont">Test223ddsd</FontFamily>
</Window.Resources>
<TextBlock Text="{Binding Source={StaticResource CustomFont},Path=Source}"></TextBlock>
//RelativeSource绑定
//绑定静态资源
  <TextBlock Text="{Binding Path=Title,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"></TextBlock>
//绑定窗体的属性      Mode=FindAncestor 绑定父元素{设置了这个属性需要搭配:AncestorType父元素的类型进行使用,AncestorLevel 父元素的层级 } ,PreviousData 绑定数据列表的前一项 ,self绑定到自身,  TemeplateParent绑定到应用数据模板的元素


 <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path=Source}"></TextBlock>
 <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path=LineSpacing}"></TextBlock>
<TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path=FamilyTypefaces[0].Style}"></TextBlock>
//绑定类型基本一直 重复写麻烦  可使用datacontext 
<StackPanel Name="stackpanel1" Margin="5" Grid.RowSpan="2" DataContext="{x:Static SystemFonts.IconFontFamily}">
<TextBlock Text="{Binding  Path=Source}"></TextBlock>
<TextBlock Text="{Binding  Path=LineSpacing}"></TextBlock>
<TextBlock Text="{Binding  Path=FamilyTypefaces[0].Style}"></TextBlock>
</StackPanel>
//在父元素设置DataContext即可  子元素不需要再指定Sourse属性直接使用父元素中已经绑定的数据,代码更简洁

WPF资源

<Window.Resources>
        <ImageBrush x:Key="tileimg" Viewport="0 0 36 36" ViewportUnits="Absolute" TileMode="Tile" ImageSource="/face.jpg" ></ImageBrush>
</Window.Resources>
 <Button Content="btn1" Foreground="Beige" Background="{StaticResource tileimg}" Margin="5" Padding="5"></Button>
 <Button Content="btn3" Margin="5" Padding="5" Background="{DynamicResource tileimg}"></Button>
 //定义一个画刷 作为button的背景颜色  这里可以使用静态资源或者动态资源进行绑定
<StackPanel.Resources>
<ImageBrush x:Key="tileimg" Viewport="0 0 36 36" ViewportUnits="Absolute" TileMode="Tile" ImageSource="/face.jpg" ></ImageBrush>
</StackPanel.Resources>
//资源可以定义在window中或者stackpanel中       
//`注:资源的定义要在使用之前` 若元素内部定义资源名称与父元素所定义的资源名称相同时 元素内部所定义资源的优先级高于父元素
//动态资源会在该资源发生变化时更新,静态资源只会在程序运行时加载一次,若资源发生变化 不会影响静态资源元素
//全局资源一般定义在app.xaml文件中
 <Button Content="btn2" Margin="5" Foreground="{DynamicResource {x:Static SystemColors.WindowTextBrush} }" Padding="5"></Button>
 <Button Content="btn2" Margin="5" Foreground="{x:Static SystemColors.WindowTextBrush}" Padding="5"></Button>
//静态绑定系统资源与动态绑定

资源字典-【类似于前端的css的外链式】

  <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ImageBrush x:Key="tile"  Viewport="0 0 32 32" ViewportUnits="Absolute" TileMode="Tile" ImageSource="/face.jpg" Opacity="0.3"></ImageBrush>
</ResourceDictionary>
//创建一个资源字典文件
<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="Window6.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
//在app.xaml文件中引入该资源字典
  <Grid Background="{DynamicResource tile}">
    </Grid>
//在窗体中使用资源字段
//在项目中使用另一个项目的资源文件
 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri("WpfLibrary1;component/Dictionary1.xaml",UriKind.Relative);
            var xx= resourceDictionary["tile"];
            var xx1 = resourceDictionary[0];
            this.grid.Background =(ImageBrush) resourceDictionary["tile"];
        }

image-20220317223615142

posted @ 2022-03-18 15:49  Wilson_it  阅读(43)  评论(0编辑  收藏  举报