随笔 - 48  文章 - 0 评论 - 9 trackbacks - 0

本文专用于记录WPF开发中的小细节,作为备忘录使用。

 

1. 关于绑定:

Text ="{Binding AnchorageValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

 

2. 关于ListBox的样式

 <ListBox.ItemContainerStyle>
     <Style TargetType="{x:Type ListBoxItem}">
           <Style.Resources>
                   <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                   <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
           </Style.Resources>
                   <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                   <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
     </Style>
 </ListBox.ItemContainerStyle>

 

3. DataGrid数据添加折叠

    Resource:

<UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="GridGroupHeadStyle" TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander x:Name="GroupExpander" IsExpanded="True" >
                                <Expander.Header>
                                    <TextBlock Foreground="SlateBlue" FontSize="12">
                                        <TextBlock.Text>
                                            <MultiBinding StringFormat="{}{0} ({1})">
                                                <Binding Path="Name"/>
                                                <Binding Path="ItemCount"/>
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
         </ResourceDictionary>
    </UserControl.Resources>

       DataGrid:  需要注意的是DataGrid的ItemsSource绑定的是ViewModel中的Groups不是Info

<DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GridGroupHeadStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
 </DataGrid.GroupStyle>

        ViewModel: 这里的PropertyGroupDescription填写的是你分组所依据的字段,并且是存在于<Model>中的。

public ObservableCollection<Model> Info { get; set; }

public ListCollectionView Groups { get; set; }


Groups = new ListCollectionView(Info);
Groups.GroupDescriptions.Add(new PropertyGroupDescription("Your Group Name"));

 

4. KeyboardNavigation.TabIndex

 <TextBlock   
      Grid.Row="0"  
      Grid.Column="2"
      VerticalAlignment="Center" 
      Height="16" Margin="0,17" KeyboardNavigation.TabIndex="4" >
      <Hyperlink x:Name="registerHyperlink" Click="Hyperlink_Click" KeyboardNavigation.TabIndex="5">
             <Run Text="点此注册"/>
      </Hyperlink>
 </TextBlock>

 

5. Button设置IsDefault=“true”则可以按Enter键触发这个按钮

posted on 2017-01-01 11:07 airforce094 阅读(...) 评论(...) 编辑 收藏