感觉学习的东西必须做个记录,不然很快就忘掉了。现在把WPF学习过程中一些零碎的东西记录在下面,没有具体的主题,想到啥、看到啥都写在这里,算是复习一下并做个备忘吧。

1. 等待对话框

看到同事做的项目中需要一个等待对话框大致如下:

WPF那些事儿

同事用定时器实现了这个效果,实际在WPF中,这个效果可以很容易通过动画实现。

<Grid>
    <TextBlock x:Name="searchTextBlock"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               FontFamily="微软雅黑"
               FontSize="24">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="TextBlock.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <StringAnimationUsingKeyFrames Duration="0:0:1.6"
                                                       RepeatBehavior="Forever"
                                                       Storyboard.TargetName="searchTextBlock"
                                                       Storyboard.TargetProperty="(TextBlock.Text)">
                            <DiscreteStringKeyFrame KeyTime="0:0:0" Value="正在查询" />
                            <DiscreteStringKeyFrame KeyTime="0:0:0.4" Value="正在查询." />
                            <DiscreteStringKeyFrame KeyTime="0:0:0.8" Value="正在查询.." />
                            <DiscreteStringKeyFrame KeyTime="0:0:1.2" Value="正在查询..." />
                        </StringAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>
</Grid>

2. XAML中的特殊字符

2.1 在XAML中设置“{”和"}"

WPF那些事儿

3. 静态资源当作元素使用

WPF那些事儿

如果不设置x:Shared="false"(默认情况x:Shared="true"),这个资源将只有一个实例,不能使用多次(因为不能是多个Visual对象的子元素或者CompositionTarget的根元素)

4. 同步选中项

如果多个选择控件(ListBox、ComboBox)绑定到同一个集合,可以通过设置"IsSynchronizedWithCurrentItem"让这些控件的选中项保持一致,但是如果有多个选中项,则目前只有第一项会保持一致

<StackPanel>
    <ListBox DisplayMemberPath="Name"
             IsSynchronizedWithCurrentItem="True"
             ItemsSource="{Binding Students}" />
    <ListBox DisplayMemberPath="Age"
             IsSynchronizedWithCurrentItem="True"
             ItemsSource="{Binding Students}" />
    <ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Students}" />
</StackPanel>
同步选中项

相关文章:

猜你喜欢
相关资源
相似解决方案