WPF学习ing~

2013/3/5

 

《深入浅出WPF》,已看到P105。

WPF让一个Canvas控件隐藏的时候不占用空间:控件名.Visibility = Visibility.Collapsed(Collapsed 不占布局大小,而Hidden 占布局,这是这两者唯一的区别。)

 

 

晚上加班改BUG,改完接着看书,P132的话讲的很好 P134页讲属性讲的不错。

 

 

2013/3/8

在类声明时使用partial(局部类&分布类)关键字可以把一个类拆在多处定义,只要各个部分代码不冲突即可。

Citas+中多个文件例如PolicyManagementModel.PaymentManagementPartial.vb、PolicyManagementModel.PolicyInsuredAndBeneficiary.Partial.vb等

就是PolicyManagement.xaml的partial

PolicyManagement.xaml.vb代码中是这样声明的Partial Public Class PolicyManagement

 

 

 

2013.05.07

在项目中修改外部定义样式的时候,发现项目中的所有样式文件全部在Resources文件夹的Styles文件夹下,一个叫AllStyle的resource dictionary文件用下面格式引用了所有样式文件

<ResourceDictionary.MergedDictionaries>
        
        <ResourceDictionary Source="/Resources/Styles/Button.xaml"/>

         <!--省略其他引用格式如上-->
</ResourceDictionary.MergedDictionaries>

 

然后在程序的APP.xaml中引用这个AllStyle样式文件

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
               
                <ResourceDictionary Source="/Resources/Themes/AllStyle.xaml"></ResourceDictionary>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

这样的话在页面上就可使用外部定义中的Style

 

举个栗子:

页面上有一个button,想用style变成圆角button等效果,这样引用style即可

<Button Name="btnSearch" Style="{StaticResource CommonButton}">

 

style如下

 

<Style x:Key="CommonButton"  TargetType="Button">
        <Setter Property="Foreground" Value="Black"/>
        <!--<Setter Property="FontSize" Value="11"></Setter>-->
        <Setter Property="FontFamily" Value="Arial,MingLiu"></Setter>
        <Setter Property="Height" Value="18"/>
        <Setter Property="Margin" Value="0,0,6,0"/>
        <Setter Property="Padding" Value="3,-2,0,3"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">

                    <Border x:Name="back" CornerRadius="6" >
                        <Border.BitmapEffect>
                            <OuterGlowBitmapEffect Opacity="0.7" GlowSize="0" GlowColor="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" />
                        </Border.BitmapEffect>
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientBrush.GradientStops>
                                    <GradientStopCollection>
                                        <GradientStop Color="#f5f5f5" Offset="0"/>
                                        <GradientStop Color="#ececec" Offset="0.5"/>
                                        <GradientStop Color="#d0d1d3" Offset="1"/>
                                    </GradientStopCollection>
                                </GradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.Background>
                        <!--前景色及边框-->
                        <Border x:Name="fore" BorderThickness="1" CornerRadius="10" BorderBrush="#aeaeae">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientBrush.GradientStops>
                                        <GradientStopCollection>
                                            <GradientStop Color="#f5f5f5" Offset="0"/>
                                            <GradientStop Color="#ececec" Offset="0.5"/>
                                            <GradientStop Color="#d0d1d3" Offset="1"/>
                                        </GradientStopCollection>
                                    </GradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Border.Background>
                            <!--按钮内容-->
                            <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding  Content}">
                            </ContentPresenter>
                        </Border>
                    </Border>
                    <!--触发器-->
                    <ControlTemplate.Triggers>
                        <!--鼠标移入移出-->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation To="6" Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />-->
                                        <ColorAnimation To="#AFFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#3FFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />-->
                                        <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                        <!--按钮按下弹起-->
                        <Trigger Property="IsPressed" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation To="3" Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />-->
                                        <ColorAnimation To="#3AAA" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#2111" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />-->
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                        <!--按钮失效-->
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="#B444"/>
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation To="0" Duration="0:0:0.3" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <DoubleAnimation To="1" Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Opacity)" />
                                        <DoubleAnimation To="-135" Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Direction)" />
                                        <ColorAnimation To="#FFF" Duration="0:0:0.3" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Color)" />-->
                                        <ColorAnimation To="#D555" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" />
                                        <ColorAnimation To="#CEEE" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#CDDD" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!--<DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Opacity)" />
                                        <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Direction)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Color)" />-->
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

 

 

 20130509

一、项目中有TabControl动态添加子项

找个简单的例子

界面:

<TabControl Height="311" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="503">
            <TabItem Header="tabItem1" Name="tabItem1">
                <Grid>
                    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="207,132,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
                </Grid>
            </TabItem>
            <TabItem Header="tabItem2" Name="tabItem2">
                <Grid />
            </TabItem>
        </TabControl>

后台:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        int item;
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
           
            ++item;
            var tab = new TabItem { Header = "tab"+item };
            tab.Name = "tab"+item;
            tabControl1.Items.Add(tab);
        }
    }

 

 二、WPF界面的切换,同Winform基本相同

 

例如登录界面,登录成功则切换到主界面,则打开新界面,关闭当前页面

 Dim mainWindow As New MainWindow
 mainWindow.Show()
 Login.Close()

 

 2013/5/31

对于同一个用户界面来说,绑定到同一个源对象上是很常见的事情。因此WPF指定了一个隐式的数据源属性,而不是显示的用Binding标记每一个控件的Source, RelativeSource或者ElementName属性,这种数据源就是数据上下文。

通常的做法是找一个常见的父类控件(属于FrameworkElement或者FrameworkContentElement类型),并设置它的DataContext属性为这个源对象,当一个绑定没有显示的源对象时,WPF会遍历整个逻辑树,直到找到一个非空的DataContext属性为止。例如:

<StackPanel x:Name="parent" DataContext="{StaticResource photos}" >

<Label x:Name="label1" Content="{Binding Path=Count}" />

...

<ListBox x:Name="pictureBox" DisplayMemberPath="Name" ItemSouce="{Binding}"/>

</StackPanel>

 

 

 

20130712

wpf datagrid 绑定datatable时 需要dt.defaultView 后才能绑定,如果不加直接绑定会出错。

posted @ 2013-03-05 19:31  yuchao.xia  阅读(471)  评论(0编辑  收藏  举报