美文网首页
WPF 资源、模板、Blend、Style

WPF 资源、模板、Blend、Style

作者: 李霖弢 | 来源:发表于2019-11-18 15:58 被阅读0次

Resources

每个UI元素都具有Resources属性。
为资源字典贴上用于检索的索引。

<Window xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <Window.Resources>
    <!--<ResourceDictionary>-->
      <sys:String x:Key="myString">hello this is String</sys:String> 
    <!--</ResourceDictionary>-->
  </Window.Resources>
  <TextBox Text="{StaticResource ResourceKey=myString}"/>
  <TextBox Text="{StaticResource myString}"/>
</Window>

或直接指定一个外部文件

<ResourceDictionary Source="A.xaml"/>

在方法中也能获取资源

String myString = this.FindResource("myString") as String;

或者已知Resources所属的元素(此处为Window所以直接this)

String myString = this.Resources["myString"] as String;
DynamicResource 动态方式使用资源
<TextBox Text="{DynamicResource myString}"/>

图片资源

<Image x:Name="myImage" Source="Resource/0.jpg"/>
<Image x:Name="myImage" Source="pack://application:,,,/Resource/0.jpg"/>
...
myImage.Source = new BitmapImage( new Uri(@"Resource/0.jpg",UriKind.Relative));
myImage.Source = new BitmapImage( new Uri(@"pack://application:,,,/Resource/0.jpg",UriKind.Absolute));

UserControl

自定义的用户控件,可以直接在同一解决方案中使用

<local:MyUserControl Loaded="MyUserControl_Loaded"/>

模板

ControlTemplate 与 Blend

决定控件外观,每个UI控件本身也是一个UI元素树

创建某UI控件的自定义副本
  • 通过FindName方法可以查找ControlTemplate
<Window.Resources>
    <ResourceDictionary>
        <sys:String x:Key="str">
            hello
        </sys:String>
        <ControlTemplate x:Key="cTmp">
          <StackPanel>
            <TextBlock x:Name="tb1" Text="I am cTmp1"></TextBlock>
            <TextBlock x:Name="tb2" Text="I am cTmp2"></TextBlock>
          </StackPanel>
        </ControlTemplate>
    </ResourceDictionary>
</Window.Resources>
<UserControl x:Name="uc" Template="{StaticResource cTmp}" Grid.Column="0" Grid.Row="2"></UserControl>
...
TextBox tb = uc.Template.FindName("tb1",uc) as TextBox;
StackPanel sp = tb.Parent as StackPanel;
(sp.Children[1] as TextBox).Text = "change cTmp2";
DataTemplate

决定数据外观
通过DataType为DataTemplate设置应用的数据类型

<Window.Resources>
    <converters:StatusConvert x:Key="statusConverter"/>
    <DataTemplate x:Key="DeviceItemsItemTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Name}" Width="400"  FontSize="25"/>
            <TextBlock Text="{Binding Status,Converter={StaticResource statusConverter}}" FontSize="25" Margin="140,0,0,0"/>
        </StackPanel>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:Human}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Age}" Width="400"  FontSize="25"/>
        </StackPanel>
    </DataTemplate>
    <c:ArrayList x:Key="ds">
        <local:Human Age="18"/>
        <local:Human Age="20"/>
        <local:Human Age="21"/>
    </c:ArrayList>
</Window.Resources>
<ListBox x:Name="devicePanel" Grid.Row="1" ItemsSource="{Binding DeviceItems}" ItemTemplate="{DynamicResource DeviceItemsItemTemplate}"/>
<ListBox Grid.Row="2" ItemsSource="{StaticResource ds}"/>

DataTemplate既能控制数据又能控制UI,

  • 通过FindName方法可以查找UI内容(同ControlTemplate)
  • 通过正常方法可直接找到数据内容

Style

通过Style设置应用的元素,若不设x:Key只设TargetType则所有该类型均获得,设置Style="{x:Null}"可禁用所有

<Window.Resources>
  <Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
    <Setter Property="Width" Value="50"></Setter>
  </Style>   
</Window.Resources>
<Button Style="{x:Null}"/>
<Button Style="{StaticResource myBtnStyle}"/>
Setter 设置成员的静态外观
Trigger 设置成员行为风格
<Style TargetType="CheckBox">
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="true">
            <!--<Trigger.Setters>-->
                <Setter Property="Foreground" Value="Red"></Setter>
            <!--</Trigger.Setters>-->
        </Trigger>
    </Style.Triggers>
</Style>
...
<CheckBox Content="hello Checkbox" Grid.Column="1" Grid.Row="2"></CheckBox>
  • MultiTrigger设置多个条件同时满足时才触发
<Style TargetType="CheckBox">
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsChecked" Value="true"/>
                <Condition Property="Content" Value="hello Checkbox"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Foreground" Value="Red"></Setter>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

此外还有DataTriggerMultiDataTrigger,由数据是否满足条件来驱动,EventTrigger由事件(鼠标、键盘或自定义)触发

相关文章

网友评论

      本文标题:WPF 资源、模板、Blend、Style

      本文链接:https://www.haomeiwen.com/subject/pqejictx.html