在WPF中创建UserControl时,我发现给它一些任意的Height和Width值很方便,这样我就可以在Visual Studio设计器中查看我的更改.但是,当我运行控件时,我希望高度和宽度未定义,以便控件将展开以填充我放入的任何容器.如何在不必删除高度和宽度值之前实现相同的功能建立我的控制?(或者不在父容器中使用DockPanel.)
以下代码演示了此问题:
<Window x:Class="ExampleApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:ExampleApplication3"
Title="Example" Height="600" Width="600">
<Grid Background="LightGray">
<loc:UserControl1 />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
以下定义UserControl1在设计时合理显示,但在运行时显示为固定大小:
<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid Background="LightCyan" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
以下定义UserControl1在设计时显示为点,但Window1在运行时展开以填充父级:
<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="LightCyan" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
如何定义MenuItem.Icon以便将MenuItemHeader文本放在菜单项图像下面?感谢您的帮助!
我有一个像这样的wpf按钮:
<Button Click="button1_Click" Height="23" Margin="0,0,5,0" Name="button1" Width="75">Initiate</Button>
Run Code Online (Sandbox Code Playgroud)
我想将pass {Binding Code}作为参数传递给button1_click处理程序.
我该怎么做?
免责声明:WPF真的很新
我想TaskBar在窗户启动时将窗口显示在时钟顶部.
如何找到桌面右下角的位置?
我使用此代码在Windows窗体应用程序中运行良好,但在WPF中无法正常工作:
var desktopWorkingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个Grid包含两行的用户控件.标题的第一行和第二行的内容将在用户控件之外定义,例如Button在我们的示例中.
不知何故,我没有让它工作.
UserControl1 xaml:
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
MainWindow xaml:
<Grid>
<local:UserControl1>
<Button>Click me</Button>
</local:UserControl1>
</Grid>
Run Code Online (Sandbox Code Playgroud)
下面的图片应该解释我的问题:

我想在WPF Expander Header上应用一种样式.在下面的XAML中,我有一个Expander,但它的风格不仅适用于标题.
谢谢.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640"
>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Expander">
<Style.Resources>
<LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#EF3132" Offset="0.1" />
<GradientStop Color="#D62B2B" Offset="0.9" />
</LinearGradientBrush>
</Style.Resources>
<Setter Property="Background" Value="{StaticResource BackBrush}"/>
</Style>
</StackPanel.Resources>
<Expander>
<StackPanel>
<TextBlock>Bike</TextBlock>
<TextBlock>Car</TextBlock>
<TextBlock>Truck</TextBlock>
</StackPanel>
</Expander>
</StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud) 我在XAML中有以下两个按钮:
<Button Content="Previous"
Margin="10,0,0,10"/>
<Button Content="Next"
Margin="0,0,10,10"/>
Run Code Online (Sandbox Code Playgroud)
如何将"10"定义为变量,以便我可以在一个地方更改它,如下所示:
PSEUDO代码:
<variable x:key="theMargin"/>
<Button Content="Previous"
Margin="{Variable theMargin},0,0,{Variable theMargin}"/>
<Button Content="Next"
Margin="0,0,{Variable theMargin},{Variable theMargin}"/>
Run Code Online (Sandbox Code Playgroud) 我创建了一个全局热键来通过PInvoking显示一个窗口RegisterHotKey().但要做到这一点,我需要那个窗口HWND,在窗口加载之前不存在,这意味着第一次显示.但我不想在设置热键之前显示窗口.有没有办法创建一个HWND对用户不可见的窗口?
我有一些文本在运行时显示在文本块中.我希望字体大小可以填充给定的区域.我认为我已正确设置文本块以"自动调整大小",我尝试增加字体大小,直到文本块大于其父级,然后将字体大小减小1.问题是我无法控制重绘/重新计算它的大小.
这是更好的方法吗?或者有没有办法让我的方法有效?
在System.Windows.Controls,我可以看到一个PrintDialog 然而,我似乎无法找到一个本地人FileDialog.我是否需要创建引用System.Windows.Forms或是否有其他方法?