深入浅出WPF 总结

根据书的示例,写了sample code。code代码上传到网易云盘,IDE是vs2013

点击打开链接

将书的一些内容总结

1. ContentPresenter通常出現在ControlTemplate內, 且若不使用ContentPresenter則Content屬性就無法正常顯示。

2. 标记扩展:为属性赋值时,使用其他控件的某个属性 (P24)
        <StackPanel Orientation="Vertical">
            <Slider Name="slider1" />
            <TextBlock Text="{Binding Value, ElementName=slider1}" />
        </StackPanel>
3. Name和x:Name的区别:当控件有Name属性的时候,一样。如果没有Name属性,可以为其设置Name属性。

4. x:Null

一旦设定为某种控件设置了默认的Style,当不想用这种style的时候,可以使用 style="{x:NULL}"

        <StackPanel Orientation="Vertical">
            <Button>1 button</Button>
            <Button>2 button</Button>
            <Button>3 button</Button>
            <Button Style="{x:Null}">3 button</Button>
        </StackPanel>
5. DisplayMemberPath, SelectedValuePath,SelectedValue(P56)

SelectedValuePath和SelectedValue配合使用。设好SelectedValuePath,选中项目后,SelectedValue会按设定好的取值。


6. 模板
https://blog.csdn.net/jfyy/article/details/80620327

https://blog.csdn.net/jfyy/article/details/80743009


例子:

1. Header类型的控件

        <GroupBox>
            <GroupBox.Header>
                <Image Source=".\Resource\1.png" Height="20"></Image>
            </GroupBox.Header>
            <TextBlock>This is the content.</TextBlock>
        </GroupBox>   

2. ListBox的SelectedValuePath和DispalyMemberPath

    <StackPanel>
        <ListBox ItemsSource="{Binding Humans}" SelectedValuePath="ID" x:Name="List1" DisplayMemberPath="Name" Height="200">
        </ListBox>
        <TextBox Text="{Binding SelectedValue, ElementName=List1}"></TextBox>
    </StackPanel>

3. RelativeSource中的AncestorType(往父节点上找)

<StackPanel x:Name="Stack11" Grid.Column="1">
            <Grid x:Name="Grid22">
                <StackPanel x:Name="Stack22">
                    <Grid x:Name="Grid33">
                        <StackPanel x:Name="Stack33">
                            <TextBlock Text="{Binding Name, RelativeSource={RelativeSource AncestorType={x:Type Grid}, AncestorLevel=2}}"/>
                            <Button>Down Message</Button>
                        </StackPanel>
                    </Grid>
                </StackPanel>
            </Grid>
        </StackPanel>

4. RelativeSource中的Self

<Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="abc">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="10"/>
                </DataTrigger>
            </Style.Triggers>

5. 动态静态资源

  <Window.Resources>
        <core:String x:Key="res1">"海上生明月"</core:String>
        <core:String x:Key="res2">"海上生明月"</core:String>
    </Window.Resources>
    <StackPanel>
        <TextBox Text="{StaticResource ResourceKey=res1}"></TextBox>
        <TextBox Text="{DynamicResource ResourceKey=res2}"></TextBox>
        <Button Click="Button_Click">Update</Button>
    </StackPanel>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Resources["res1"] = "天涯共此时";
            Resources["res2"] = "天涯共此时";
        }

6. 布局中还有比较重要的WrapPanel(流式布局)和DockPanel
<WrapPanel>
        <Button Width="60" Content="Button"/>
        <Button Width="60" Content="Button"/>
        <Button Width="60" Content="Button"/>
        <Button Width="60" Content="Button"/>
        <Button Width="60" Content="Button"/>
        <Button Width="60" Content="Button"/>
    </WrapPanel>
    <DockPanel>
        <TextBox DockPanel.Dock="Top" Text="Top"/>
        <TextBox DockPanel.Dock="Left" Text="Left"/>
        <TextBox Text="Other"/>
    </DockPanel>

7. Style (写在resource里)

无Trigger:

        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="24"/>
            <Setter Property="FontStyle" Value="Italic"/>
        </Style>

属性Trigger:

        <Style TargetType="CheckBox">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="true">
                    <Setter Property="FontSize" Value="24"/>
                    <Setter Property="FontStyle" Value="Italic"/>
                </Trigger>
            </Style.Triggers>
        </Style>

数据Trigger:

        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="abc">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="10"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

多条件Trigger:

 <Style TargetType="CheckBox">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsChecked" Value="true"/>
                        <Condition Property="Content" Value="点我才行"/>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="FontSize" Value="24"/>
                        <Setter Property="FontStyle" Value="Italic"/>
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>

事件Trigger:

 <Style TargetType="Button">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.300" Storyboard.TargetProperty="FontSize" To="28" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>

                <EventTrigger RoutedEvent="MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.800" Storyboard.TargetProperty="FontSize" To="18" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>

展开阅读全文

没有更多推荐了,返回首页