小编Geo*_*Cox的帖子

ListBox与ListView - 如何选择数据绑定

我正在考虑使用ListBox或ListView作为WPF应用程序.似乎要么支持数据绑定和项目模板.我的应用程序有一个简单的项目列表,我打算能够根据用户输入搜索/排序/过滤.数据绑定演示(http://msdn.microsoft.com/en-us/library/ms771319.aspx)使用带有CollectionViewSource的ListBox.

有没有人有哪些控制权可以使用以及何时使用?

data-binding wpf listview listbox

317
推荐指数
1
解决办法
11万
查看次数

释放鼠标捕获并让鼠标单击通过

我有一个类似于弹出窗口或菜单的控件.我想显示它,当用户点击框的边界外时,让它隐藏起来.我已经使用了Mouse.Capture(这个,CaptureMode.SubTree)以及在OnLostMouseCapture中以与Menu/Popup相同的方式重新获取捕获.

当用户在控件的边界外单击时,我在OnPreviewMouseDown中释放鼠标捕获.我没有把e.Handled设置为true.鼠标单击将转到主UI上的其他控件,但不会转到窗口的关闭按钮(红色X).它需要2次点击才能关闭应用.

有没有办法告诉WPF重新启动鼠标点击,或发送重复的鼠标点击事件?

这是我的代码.注意我将其重命名为MainMenuControl - 我没有构建菜单,因此Menu/MenuItem和Popup不是选项.

public class MainMenuControl : Control
    {
        static MainMenuControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MainMenuControl), new FrameworkPropertyMetadata(typeof(MainMenuControl)));
        }

        public MainMenuControl()
        {
            this.Loaded += new RoutedEventHandler(MainMenuControl_Loaded);

            Mouse.AddPreviewMouseDownOutsideCapturedElementHandler(this, OnPreviewMouseDownOutsideCapturedElementHandler);
        }

        void MainMenuControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(MainMenuControl_IsVisibleChanged);
        }

        void MainMenuControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.IsVisible)
            {
                Mouse.Capture(this, CaptureMode.SubTree);
                Debug.WriteLine("Mouse.Capture");
            }
        }

        // I was doing this in OnPreviewMouseDown, but changing to this didn't have any effect
        private void OnPreviewMouseDownOutsideCapturedElementHandler(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("OnPreviewMouseDownOutsideCapturedElementHandler");

            if (!this.IsMouseInBounds())
            { …
Run Code Online (Sandbox Code Playgroud)

wpf mouse popup capture

10
推荐指数
1
解决办法
7006
查看次数

你能在数据绑定的WPF样式中做"数学"吗?

我有一个按钮控件样式,我想从数据绑定版本更改填充,以调整需要2像素偏移的字形.我将使用SimpleStyles.xaml中的SimpleButton作为示例(...显示为了简洁而删除触发器代码的位置):

<Style x:Key="SimpleButton" TargetType="{x:Type Button}" BasedOn="{x:Null}">
  <Setter Property="FocusVisualStyle" Value="{DynamicResource SimpleButtonFocusVisual}"/>
  <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
  <Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <!-- We use Grid as a root because it is easy to add more elements to customize the button -->
           <Grid x:Name="Grid">
             <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/>

              <!-- Content Presenter is where the text content etc is placed by the control. The bindings are useful so that the control can be …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf templates styles

9
推荐指数
1
解决办法
6627
查看次数

你能通过WPF样式数据绑定到CornerRadius吗?

我有一个Button样式,似乎无法将边框的CornerRadius属性数据绑定到模板.这是一个依赖属性,因此它应该是数据可绑定的.我想知道我是否缺少使用正确的XAML语法?

<Style TargetType="{x:Type Button}" BasedOn="{x:Null}">         
      <Setter Property="FocusVisualStyle" Value="{DynamicResource MyButtonFocusVisual}"/>       
      <Setter Property="Background" Value="{DynamicResource MyButtonBackgroundBrush}"/>       
      <Setter Property="Foreground" Value="{DynamicResource MyButtonForegroundBrush}"/>
      <Setter Property="BorderBrush" Value="{DynamicResource MyButtonBorderBrush}"/>
      <Setter Property="BorderThickness" Value="3"/>
      <Setter Property="FontFamily" Value="Segoe UI"/>      
      <Setter Property="FontSize" Value="14" />
      <Setter Property="CornerRadius" Value="2" />
      <Setter Property="Template">          
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
           <!-- We use Grid as a root because it is easy to add more elements to customize the button -->
           <Grid x:Name="Grid">
           <Border x:Name="Border" CornerRadius="{TemplateBinding CornerRadius}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/> 
           </Grid> 
         </ControlTemplate>             
       </Setter.Value>      
     </Setter> …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf styles cornerradius

2
推荐指数
1
解决办法
2400
查看次数