标签: wpf-controls

自定义控件依赖项属性绑定

即使是最基本的例子,我也会疯狂地试图让它工作.我不能为我的生活得到约束力.这是一个不适合我的超级简单示例.我必须做错事.

我的控件库程序集中的自定义控件:

public class TestControl : Control
{
    public static readonly DependencyProperty TestPropProperty =
        DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null));

    public string TestProp
    {
        get { return (string)GetValue(TestPropProperty); }
        set { SetValue(TestPropProperty, value); }
    }

    static TestControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
    }
}
Run Code Online (Sandbox Code Playgroud)

它的XAML模板:

<Style TargetType="{x:Type local:TestControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TestControl}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>
                        <TextBlock Text="Testing..." />
                        <Label Content="{Binding TestProp}" Padding="10" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这是XAML在wpf窗口中使用控件来引用我的控件库:

<Grid>
    <ItemsControl Name="mylist">
        <ItemsControl.ItemTemplate>
            <DataTemplate> …
Run Code Online (Sandbox Code Playgroud)

wpf dependency-properties wpf-controls

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

处理可编辑的分层数据/ TreeView~DataGrid混合

我正在寻找一个WPF控件,它是TreeView和DataGrid的混合体,类似于Visual Studio调试器或QuickBooks联系人列表等.

关于如何在WPF中处理可编辑分层数据的任何其他解决方案也将非常受欢迎.

在此输入图像描述

wpf treeview controls datagrid wpf-controls

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

将WPF样式应用于多个控件

这个问题可能是重复的,但我在SO上找不到它.

如果我有一个容器Window,StackPanel,Grid等有没有什么办法可以应用Style到包含在其中的某一类型的所有控件?

我可以通过使用Container.Resources和设置个别更改来应用属性更改,TargetType但是当我尝试设置Style目标时,我收到错误,告诉我无法设置Style.

有没有办法在XAML中这样做?

.net xaml styles wpf-controls

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

使用WPF最小化应用程序到系统托盘(不使用NotifyIcon)

我完成了我的应用程序,现在我希望将"最小化到系统托盘功能"中.我读了一篇很好的文章, 最小化app到系统托盘.我意识到这些都使用了Windows.Form类.

不幸的是,我使用Windows Presentation Foundation WPF参考来制作我的应用程序UI.现在我看到WPF不支持NotifyIcon.我看到CodePlex上有一个开源库,模拟NotifyIcon属性WPF Contrib我尚未使用它.

现在我正在解决问题.以下是我的问题: -

a)我不想仅为一个组件合并第三方库.

b)我可以在WPF上没有NotifyIcon的情况下进行最小化功能吗?如果是,那么有人可以给我带来什么样的优惠?

或者也许我应该将我的UI恢复为使用Windows窗体?

c# windows wpf notifyicon wpf-controls

21
推荐指数
1
解决办法
4万
查看次数

如何让WPF窗口自动调整内容,而不是更多

我有一个包含2个TextBlocks,一个进度条和一个取消按钮的对话框.

这是XAML:

Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication4"
    mc:Ignorable="d"
    Title="MainWindow" Height="Auto" Width="200">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TextBlock x:Name="txtFirst" Grid.Row="0"  Margin="5" TextWrapping="Wrap">This is a really really really really long string that wraps</TextBlock>
    <TextBlock x:Name="txtSecond" Grid.Row="1"  Margin="5" Text="A Shorter string" TextWrapping="Wrap" MaxWidth="200"/>
    <ProgressBar x:Name="prgProgress" Grid.Row="2" Margin="5" Height="20" />
    <Button x:Name="btnCancel" Grid.Row="3" Margin="5" Height="25" Width="50"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我希望Window不要有一个固定的高度,但是根据它的孩子的大小自动调整它的高度而不是更多,但是看不到这样做的方法.在我没有为Window的高度指定任何东西的那一刻,它似乎采用了一个比内容更大的高度不确定原因,或者它从哪里获得高度值?如果我设置Windows高度="自动",我会得到相同的东西.RowDefinitions的所有高度都设置为"Auto",我的意思是"将行高设置为行子高度".

非常感谢您的帮助.

c# wpf layout xaml wpf-controls

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

WPF中的设计效果(增强应用程序外观的技巧)

我开发了一个必须在展览会上作为广告展示的应用程序.我希望它看起来更性感!您知道哪些技巧可以增强应用程序的外观?

开发人员可以为其应用程序使用哪些最佳设计效果?我说的是:发光,阴影,可能是按钮的形式,漂亮的闪屏动画等等.

你最喜欢的效果是什么?

c# wpf wpf-controls

20
推荐指数
3
解决办法
2万
查看次数

如何在动态创建的ContextMenu中添加水平分隔符?

我在互联网上寻找解决方案,但无法在我的样本中找到它.我需要在从代码隐藏生成的Context菜单项之间添加一个分隔符.我尝试使用如下代码行添加它但没有成功.

this.Commands.Add(new ToolStripSeparator()); 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人可以提供帮助.先感谢您.

上下文菜单XAML:

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu ItemsSource="{Binding Commands}">
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding}" />
                        <Setter Property="Header" Value="{Binding Path=Text}" />
                        <Setter Property="CommandParameter" Value="{Binding Path=Parameter}" />
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </Setter.Value>
    </Setter>
Run Code Online (Sandbox Code Playgroud)

在方法中添加的C#:

this.Commands = new ObservableCollection<ICommand>();
        this.Commands.Add(MainWindow.AddRole1);
        this.Commands.Add(MainWindow.AddRole2);
        this.Commands.Add(MainWindow.AddRole3);
        this.Commands.Add(MainWindow.AddRole4);
        //this.Add(new ToolStripSeparator()); 
        this.Commands.Add(MainWindow.AddRole5);
        this.Commands.Add(MainWindow.AddRole6);
        this.Commands.Add(MainWindow.AddRole7); 
Run Code Online (Sandbox Code Playgroud)

wpf binding wpf-controls wpfdatagrid

20
推荐指数
3
解决办法
2万
查看次数

WPF自定义控件生命周期图?

我正在寻找一个图表,作为参考,显示事件和覆盖的顺序,这些事件和覆盖调用WPF自定义控件,这些控件派生自通用类,如FrameworkElement,UIElement,Control,ContentControl,Decorator等.

我对控制过程变得可见并准备好与之交互的事件和覆盖特别感兴趣.换句话说,我对这个图表并不感兴趣,这个图表显示了可以处理用户在准备好与之交互后与之交互的事件,例如MouseLeftButtonDown.

我正在寻找的是像这个图(这些图解释了WPF 窗口应用程序的生命周期),但用于自定义控件.

我也看过这个页面,在我看来这看起来太简单了.该网页似乎基本上说,有大多只是Initialized,LoadedUnloaded事件.为了说明,我需要在自定义控件中使用的一些常规事件/覆盖包括:

  • 构造函数
  • Loaded
  • UnLoaded
  • IsVisibleChanged
  • OnApplyTemplate

随着稍微更专业的覆盖,如Popup OnOpened和Expander OnExpanded等.

我遇到的问题是我忘记了什么时候能做的事情VisualStateManager.GoToState(...)(这OnApplyTemplate是我相信的第一个真正的机会).而且我还会发现我需要使用的事件,例如IsVisibleChanged(当用户返回到包含我的控件的Tab时,当我需要一个控件来执行"重新加载/刷新"状态更改时,此事件被证明是必要的) .所以我一直想知道还有其他这样的事件/覆盖,我真的要注意自定义控件吗? 这是一个显示这些事物相互关联的图表的地方.

考虑到这些类型的事件/覆盖,这个Silverlight图表非常接近我正在寻找的,但对于WPF.该帖子上的一些评论者说,WPF的事件/覆盖与Silverlight不同. 如果是这样,你能指点我一个类似的图表吗?

为了进一步说明对这种图的需求,请考虑我在源自ContentControl的自定义控件上目睹的以下事件序列:

  1. 应用程序启动了.(我的自定义控件与初始选项卡位于不同的选项卡中.)
  2. Initialized 事件回调叫
  3. Loaded 事件回调叫
  4. (我单击包含我的自定义控件的选项卡)
  5. IsVisibleChanged 事件回调叫
  6. OnApplyTemplate 覆盖被叫
  7. Loaded 事件回调叫
  8. Loaded 事件回调再次调用
  9. Loaded 事件回调连续第三次调用
  10. (控制现在完全可见并准备好与之交互)

wpf event-handling wpf-controls

20
推荐指数
1
解决办法
5734
查看次数

WPF - 使用鼠标事件在画布上绘图

我在画布上处理鼠标事件时遇到问题.我想用鼠标绘制它,我想出了这些事件处理程序,但是当我开始绘图时它们什么都不做.

    private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void paintSurface_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }
Run Code Online (Sandbox Code Playgroud)

你能告诉我什么缺失或者如何重写它以帮助我开始工作吗?

c# wpf wpf-controls

20
推荐指数
2
解决办法
5万
查看次数

阻止WPF TextBox随着文本的增长而停止

我试图修改我的简单控件,以便文本框不会随着用户键入长文本而增长.我看了一下在Stackoverflow上发布的一些解决方案,建议使用Grid和一个不可见的Border并将文本框的宽度绑定到Border的ActualWidth,但我似乎无法在我的设置中使用它.

这是我控制的xaml:

<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
  <TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
  <TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
        BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
  <StackPanel Orientation="Horizontal">
    <Image Source="..\Resources\zoom.png" Width="13"/>
    <TextBox Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
  </StackPanel>
</Border>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我需要zoom.png出现在文本框的"内部",所以我使用水平堆栈面板,只是将图像和文本框并排放置,两者都被相同的边框包围.

有没有办法让我在输入文字时停止自动增长的文本框?

谢谢.

更新:

下面是我正在测试的xaml.

<Window x:Class="Desktop.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:composite="http://www.codeplex.com/CompositeWPF"
    Title="MyShell" Height="50" Width="900"
    WindowStyle="None"
    ShowInTaskbar="False"        
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="CanResizeWithGrip"
    WindowStartupLocation="CenterScreen">
  <Border BorderBrush="Black"
                BorderThickness="1.5"
                CornerRadius="5"
                Background="Gray">
    <ItemsControl composite:RegionManager.RegionName="MainRegion">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <WrapPanel></WrapPanel>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
    </ItemsControl>
  </Border>
</Window>


<UserControl x:Class="Desktop.SearchTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="50" Margin="0">
  <StackPanel Margin="5,0">
    <WrapPanel …
Run Code Online (Sandbox Code Playgroud)

wpf wpf-controls

19
推荐指数
3
解决办法
2万
查看次数