我想更改我的窗口模板,例如:
<Style x:Key="SilverGreenWindowStyle" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="{StaticResource SilverGreenBackground}" Width="503" Height="383">
<Rectangle Margin="192,86,21,119" Fill="{StaticResource SilverGreenRectangleBackground}" Width="200" Height="200"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
这会导致Windows控件变得不可见.如何让它们可见?
这里有一个适用于所有XAML向导:WPF Toolkit Calendar控件(2009年6月)似乎有一个错误.修改ControlTemplate日历时,只会出现错误,特别是PART_CalendarItem.
在这条消息的最后,我已经将XAML包含在一个(Blend 3.0)窗口中,该窗口声明Calendar并指定了它ControlTemplate.控件模板是Calendar控件模板的未修改副本,我通过编辑Calendar控件和PART_CalendarItem控件的控件模板(在Blend中)的副本来获得该模板.
在XAML的第78行(用下面的注释"EXCEPTION"标记),在控件的标题上VisualStateManager指定TextColor一个鼠标悬停Month.但是,在控件模板中,文本颜色被分配给Grid保存Month按钮的文本颜色,而不是月份按钮本身.当为日历分配未修改的控件模板时,这会导致VS2008和Blend 3.0中的异常,如下面的XAML中所示.
我无法弄清楚如何修改控件模板以消除错误,而不是删除鼠标悬停突出显示.我想保留它,但我不知道该TextColor属性应该针对什么.有什么建议?谢谢你的帮助!
XAML标记
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Style x:Key="CalendarStyle1" TargetType="{x:Type Custom:Calendar}">
<Setter Property="Foreground" Value="#FF333333"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE4EAF0" Offset="0"/>
<GradientStop Color="#FFECF0F4" Offset="0.16"/>
<GradientStop Color="#FFFCFCFD" Offset="0.16"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/> …Run Code Online (Sandbox Code Playgroud) 我ControlTemplate为我的自定义控件创建了一个MyControl.
MyControl派生自System.Windows.Controls.Control并定义以下属性public ObservableCollection<MyControl> Children{ get; protected set; }.
要显示嵌套的子控件,我使用的是一个被a包围的ItemsControl(StackPanel)GroupBox.如果没有子控件,我想隐藏GroupBox.
应用程序启动时一切正常:如果Children属性最初包含至少一个元素,则显示组框和子控件.在另一种情况下,它是隐藏的.
当用户将子控件添加到空集合时,问题就开始了.这种GroupBox可见性仍然崩溃了.从集合中删除最后一个子控件时会出现同样的问题.在GroupBox仍然可见.另一个症状是HideEmptyEnumerationConverter转换器没有被调用.向非空集合添加/删除子控件按预期工作.
以下绑定有什么问题?显然它可以工作一次,但不会更新,虽然我绑定的集合是类型ObservableCollection.
<!-- Converter for hiding empty enumerations -->
<Common:HideEmptyEnumerationConverter x:Key="hideEmptyEnumerationConverter"/>
<!--- ... --->
<ControlTemplate TargetType="{x:Type MyControl}">
<!-- ... other stuff that works ... -->
<!-- Child components -->
<GroupBox Header="Children"
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Children, Converter={StaticResource hideEmptyEnumerationConverter}}">
<ItemsControl ItemsSource="{TemplateBinding Children}"/>
</GroupBox>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
.
[ValueConversion(typeof (IEnumerable), …Run Code Online (Sandbox Code Playgroud) wpf binding relativesource observablecollection controltemplate
我目前正在尝试为WPF中ControlTemplate的Button类创建一个类,用通常的可视化树取代使得按钮看起来类似于Google Chrome选项卡上的小(X)关闭图标.我决定Path在XAML中使用一个对象来实现这个效果.使用属性触发器,控件通过设置图标的红色背景来响应IsMouseOver属性的更改.
这是来自测试应用程序的XAML:
<Window x:Class="Widgets.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="borderStyle" TargetType="Border">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#CC0000"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="closeButtonTemplate" TargetType="Button">
<Border Width="12" Height="12" CornerRadius="6"
BorderBrush="#AAAAAA" Background="Transparent"
Style="{StaticResource borderStyle}"
ToolTip="Close">
<Viewbox Margin="2.75">
<Path Data="M 0,0 L 10,10 M 0,10 L 10,0" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType=Border, AncestorLevel=1}}" StrokeThickness="1.8"/>
</Viewbox>
</Border>
</ControlTemplate>
</Window.Resources>
<Grid Background="White">
<Button Template="{StaticResource closeButtonTemplate}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
请注意,圆形背景始终存在 - 当鼠标悬停在圆形背景上时,它只是透明的.
这个问题是触发器无法正常工作.按钮的外观没有任何变化.但是,如果我Background="Transparent" …
我有一个textBox需要在某些情况下"转换"为DatePicker.
轻松完成模板:
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="local:MyTextBox.IsDate" Value="True">
<Setter Property="Template" Value="{StaticResource DateTextBoxTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
Run Code Online (Sandbox Code Playgroud)
然后:
<ControlTemplate x:Key="DateTextBoxTemplate" TargetType="TextBox">
<DatePicker x:Name="DateContent"
Text="{Binding RelativeSource={RelativeSource AncestorType=TextBox}, Path=Text, Mode=TwoWay}" />
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
问题是:当我点击它时,焦点不会"转移"到日期选择器.
即:如果我点击控件,则datePicker不会获得焦点.为了实现这一点,我必须再次点击它.
我知道我可以在我的代码背后做:
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (IsDate)
{
DatePicker dateContent = Template.FindName("DateContent", this) as DatePicker;
if (dateContent != null) dateContent.Focus();
}
}
Run Code Online (Sandbox Code Playgroud)
但这并不能让我满意,因为我很确定有一种方法可以在xaml中完成所有操作而我只是不知道它.
我发现另一个问题提到了这个FocusManager.FocusedElement="{Binding ElementName=DateContent}"选项,但我不知道我可以把这段代码放在哪里:它不能添加到controlTemplate(我怀疑),如果我把它放在封装datePicker的网格中模板,它基本上没用.
我只能在xaml中这样做吗?如果是,怎么样?
我有一个带有 App.xaml、MainWindow.xaml 和 Person 类的简单应用程序。当我不指定模板时,我的 ValidateOnDataErrors 工作正常,当文本框出错时,会在文本框周围放置红色边框。但是,一旦我在 MainWindow.xaml 的 Window 标记中插入“Template="{StaticResource WindowTemplate}"”,该字段仍然会被验证,但红色边框就会消失。
应用程序.xaml:
<Application x:Class="WpfPOC.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ControlTemplate TargetType="Window" x:Key="WindowTemplate">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Control x:Name="FocusCatcher"></Control>
<TextBlock>Menu Section</TextBlock>
<ContentPresenter Grid.Row="1" />
<StatusBar Height="23" VerticalAlignment="Bottom" Grid.Row="2">
<TextBlock Text="Current Editing Mode" />
</StatusBar>
</Grid>
</ControlTemplate>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
主窗口.xaml:
<Window x:Class="WpfPOC.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:WpfPOC"
Template="{StaticResource WindowTemplate}"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<data:Person x:Key="myDataSource" Name="Joe"/>
</Window.Resources>
<Grid>
<TextBox Height="23" Width="120" Text="{Binding Source={StaticResource myDataSource}, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将WPF颜色选择器的控件模板设置为矩形.我希望只显示一个矩形形状来代替ColorPicker ComboBox.但是当我在ControlTemplate中放置任何控件时,我得到的错误是"对象引用未设置为对象的实例".
这是我的wpf代码:
<wpfx:ColorPicker Name="ColorPicker1" Height="30" DisplayColorAndName="False"
Margin="29,72,366,209"
SelectedColorChanged="ColorPicker1_SelectedColorChanged">
<wpfx:ColorPicker.Template>
<ControlTemplate>
<Rectangle ></Rectangle>
</ControlTemplate>
</wpfx:ColorPicker.Template>
</wpfx:ColorPicker>
Run Code Online (Sandbox Code Playgroud)
对我做错了什么的建议?
我最近创建了一个UserControl用于WrapPanel可视化一些数据的工具。我Padding对其应用了 a 以便在每个元素之间留出一些空间。
第一个版本如下所示:
<UserControl x:Class="IFCS.EntityOverviewPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:l="clr-namespace:IFCS"
mc:Ignorable="d"
Padding="5, 5, 5, 5"
d:DesignHeight="300" d:DesignWidth="300">
<!-- Code -->
</UserControl>
Run Code Online (Sandbox Code Playgroud)
现在我只是应用了 aControlTemplate来覆盖我的Padding设置。
当前版本如下所示:
<UserControl x:Class="IFCS.EntityOverviewPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:l="clr-namespace:IFCS"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<!-- Code -->
</ControlTemplate>
</UserControl.Template>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我想再次申请Padding,UserControl但我尝试的一切都不起作用。我尝试应用一个Style使用
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Padding" Value="5, 5, 5, 5"/>
</Style>
</UserControl.Style>
Run Code Online (Sandbox Code Playgroud)
但这没有用。在“标题”中设置Padding也不起作用。
我必须在哪里设置该Padding值才能获得与第一个版本相同的结果?
我有一个"立方体"(Dice)控件,它来自Button
立方体:
public class Cube : Button
{
public Cube()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Cube), new FrameworkPropertyMetadata(typeof(Cube)));
}
...... // Stuff
}
Run Code Online (Sandbox Code Playgroud)
模板(一般):
<ControlTemplate TargetType="{x:Type local:Cube}" x:Key="CubeControlTemplate">
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border>
<Grid>
.......
</Grid>
</Border>
<Border Grid.Column="2">
<Grid>
.......
</Grid>
</Border>
<Grid>
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
它看起来像什么:
黄色标记显示它只能在内容后面点击,只有当你真正针对按钮被"隐藏"的点击时才会点击.
任何想法为什么会这样?
假设我在带有灰色背景的网格中的 WPF 应用程序中有一个简单的按钮:
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Hello" Width="100" Height="50" Background="#333333" BorderThickness="0"/>
Run Code Online (Sandbox Code Playgroud)
我想通过添加以下代码来更改我的按钮的 MouseOver 行为:
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border>
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#404040"/>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
<Grid Background="Transparent">
<ContentPresenter></ContentPresenter>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
Run Code Online (Sandbox Code Playgroud)
问题是:为什么添加这个样式标签会改变颜色和移动文本?
controltemplate ×10
wpf ×10
xaml ×5
c# ×4
.net ×1
binding ×1
color-picker ×1
exception ×1
focus ×1
wpf-controls ×1
wpftoolkit ×1