即使是最基本的例子,我也会疯狂地试图让它工作.我不能为我的生活得到约束力.这是一个不适合我的超级简单示例.我必须做错事.
我的控件库程序集中的自定义控件:
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控件,它是TreeView和DataGrid的混合体,类似于Visual Studio调试器或QuickBooks联系人列表等.
关于如何在WPF中处理可编辑分层数据的任何其他解决方案也将非常受欢迎.

这个问题可能是重复的,但我在SO上找不到它.
如果我有一个容器Window,StackPanel,Grid等有没有什么办法可以应用Style到包含在其中的某一类型的所有控件?
我可以通过使用Container.Resources和设置个别更改来应用属性更改,TargetType但是当我尝试设置Style目标时,我收到错误,告诉我无法设置Style.
有没有办法在XAML中这样做?
我完成了我的应用程序,现在我希望将"最小化到系统托盘功能"中.我读了一篇很好的文章, 最小化app到系统托盘.我意识到这些都使用了Windows.Form类.
不幸的是,我使用Windows Presentation Foundation WPF参考来制作我的应用程序UI.现在我看到WPF不支持NotifyIcon.我看到CodePlex上有一个开源库,模拟NotifyIcon属性WPF Contrib我尚未使用它.
现在我正在解决问题.以下是我的问题: -
a)我不想仅为一个组件合并第三方库.
b)我可以在WPF上没有NotifyIcon的情况下进行最小化功能吗?如果是,那么有人可以给我带来什么样的优惠?
或者也许我应该将我的UI恢复为使用Windows窗体?
我有一个包含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",我的意思是"将行高设置为行子高度".
非常感谢您的帮助.
我开发了一个必须在展览会上作为广告展示的应用程序.我希望它看起来更性感!您知道哪些技巧可以增强应用程序的外观?
开发人员可以为其应用程序使用哪些最佳设计效果?我说的是:发光,阴影,可能是按钮的形式,漂亮的闪屏动画等等.
你最喜欢的效果是什么?
我在互联网上寻找解决方案,但无法在我的样本中找到它.我需要在从代码隐藏生成的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自定义控件,这些控件派生自通用类,如FrameworkElement,UIElement,Control,ContentControl,Decorator等.
我对控制过程变得可见并准备好与之交互的事件和覆盖特别感兴趣.换句话说,我对这个图表并不感兴趣,这个图表显示了可以处理用户在准备好与之交互后与之交互的事件,例如MouseLeftButtonDown.
我正在寻找的是像这个图(这些图解释了WPF 窗口或应用程序的生命周期),但用于自定义控件.
我也看过这个页面,在我看来这看起来太简单了.该网页似乎基本上说,有大多只是Initialized,Loaded和Unloaded事件.为了说明,我需要在自定义控件中使用的一些常规事件/覆盖包括:
LoadedUnLoadedIsVisibleChangedOnApplyTemplate随着稍微更专业的覆盖,如Popup OnOpened和Expander OnExpanded等.
我遇到的问题是我忘记了什么时候能做的事情VisualStateManager.GoToState(...)(这OnApplyTemplate是我相信的第一个真正的机会).而且我还会发现我需要使用的事件,例如IsVisibleChanged(当用户返回到包含我的控件的Tab时,当我需要一个控件来执行"重新加载/刷新"状态更改时,此事件被证明是必要的) .所以我一直想知道还有其他这样的事件/覆盖,我真的要注意自定义控件吗? 这是一个显示这些事物相互关联的图表的地方.
考虑到这些类型的事件/覆盖,这个Silverlight图表非常接近我正在寻找的,但对于WPF.该帖子上的一些评论者说,WPF的事件/覆盖与Silverlight不同. 如果是这样,你能指点我一个类似的图表吗?
为了进一步说明对这种图的需求,请考虑我在源自ContentControl的自定义控件上目睹的以下事件序列:
Initialized 事件回调叫Loaded 事件回调叫IsVisibleChanged 事件回调叫OnApplyTemplate 覆盖被叫Loaded 事件回调叫Loaded 事件回调再次调用Loaded 事件回调连续第三次调用我在画布上处理鼠标事件时遇到问题.我想用鼠标绘制它,我想出了这些事件处理程序,但是当我开始绘图时它们什么都不做.
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)
你能告诉我什么缺失或者如何重写它以帮助我开始工作吗?
我试图修改我的简单控件,以便文本框不会随着用户键入长文本而增长.我看了一下在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-controls ×10
wpf ×9
c# ×4
xaml ×2
.net ×1
binding ×1
controls ×1
datagrid ×1
layout ×1
notifyicon ×1
styles ×1
treeview ×1
windows ×1
wpfdatagrid ×1