我已按照此代码片段将控件模板添加到 UpdateBtn,但收到一条错误消息,指出Triggers在 ControlTemplate 上未找到该属性。这个想法是将故事板绑定到IsEnabled按钮的属性。
Error 5 The attachable property 'Triggers' was not found in type 'ControlTemplate'.
Run Code Online (Sandbox Code Playgroud)
我研究了该错误,看来该属性是ControlTemplate的一部分的一部分,这是一个 WPF 应用程序而不是 Windows。所以不确定为什么 xaml 设计器中会显示错误。
谁能就我在实施过程中出错的地方提出建议?
按钮和关联的 ns 的 XAML 如下:
<Window x:Class="MongoDBApp.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:email_validator="clr-namespace:MongoDBApp.Validator"
Title="Orders Dashbord"
Width="800"
Height="500">
<Grid>
<TabControl>
<TabItem Header="Customer">
<Grid>
<Button x:Name="updateBtn"
Width="75"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding Path=UpdateCommand}"
Content="Update">
<Button.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock>Updates customer record</TextBlock>
</StackPanel>
</ToolTip>
</Button.ToolTip>
<Button.Template>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Content" Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
<DiscreteObjectKeyFrame …Run Code Online (Sandbox Code Playgroud) 我目前停留在通过转换器为控件分配不同模板的问题上.
所以我有2个模板.
<ControlTemplate x:Name="_templateA" x:Key="templateA">
<StackPanel Grid.Column="0" Margin="0,0,5,0">
<Blah />
</StackPanel>
</ControlTemplate>
<ControlTemplate x:Name="_templateB" x:Key="templateB">
<StackPanel Grid.Column="0" Margin="0,0,5,0">
<Blah Blah />
</StackPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我使用这个转换器控制这个:
<ControlA x:Name="_controlA" >
<Control Template="{Binding Converter={StaticResource templateConverters}}" />
</ControlA>
Run Code Online (Sandbox Code Playgroud)
我的转换器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Object a;
ControlTemplate template = null;
try
{
a= value as ObjectA;
if (value != null)
template = a.useTemplate1 ? [templateA from xaml] : [templateB from xaml];
}
catch (Exception ex)
{
Debug.Assert(false, ex.ToString());
} …Run Code Online (Sandbox Code Playgroud) 我有Listbox:
<ListBox x:Name="FriendsRequestList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel>
<TextBlock Text="{Binding FullName}" Foreground="#FF316DCB"/>
<TextBlock Text="{Binding RequestText}" />
<StackPanel Orientation="Horizontal">
<Button Name="Accept" Content="Accept" Click="Accept_Click" Foreground="#FF28901F" Background="#FFB4D8BA"/>
<Button Name="Decline" Content="Decline" Click="Decline_Click" Foreground="#FF28901F" Background="#FFB4D8BA"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</Listbox>
Run Code Online (Sandbox Code Playgroud)
我在代码中尝试这些:
private void Accept_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = sender as Button;
StackPanel st1 = clickedButton.Parent as StackPanel;
StackPanel st2 = st1.Parent as StackPanel;
StackPanel st3 = st2.Parent as StackPanel;
object parentControl = st3.Parent;
object obj = FriendsRequestList.Items[3];
int index1 = FriendsRequestList.Items.IndexOf(obj); …Run Code Online (Sandbox Code Playgroud) silverlight listbox selectedindex controltemplate windows-phone-7
在我的窗口中,我有一系列六个按钮,它们指示我的 ViewModel 属性之一的六种可能状态。需要突出显示处于活动状态的那个。为此,我为按钮创建了以下 ControlTemplate:
<ControlTemplate x:Key="SnijRichtingTemplate" TargetType="Button">
<Border Name="toggleButton" BorderThickness="1" BorderBrush="{StaticResource KleurRadioCheckOuter}" Background="Transparent" Width="20" Height="20" Cursor="Hand">
<TextBlock Name="text" Foreground="{StaticResource KleurRadioCheckOuter}"
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"
ToolTip="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.ToolTip}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualityToBooleanConverter}">
<Binding Path="SnijRichting" />
<Binding Path="Tag" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</DataTrigger.Binding>
<Setter TargetName="toggleButton" Property="BorderBrush" Value="{StaticResource KleurTekstDonker}" />
<Setter TargetName="text" Property="Foreground" Value="{StaticResource KleurTekstDonker}" />
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="toggleButton" Property="BorderBrush" Value="{StaticResource Kleur2}" />
<Setter TargetName="text" Property="Foreground" Value="{StaticResource Kleur2}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate> …Run Code Online (Sandbox Code Playgroud) 所以我有一个在wpf中创建的圆形按钮,如下所示:
<Button Grid.Column="3" Width="25" Height="25" Content="X" Margin="10,5,5,5" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse Name="Ellipse" Fill="{TemplateBinding Background}"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
但是它显示的按钮是:

为什么红色X没出现在我按钮的中央?并且可以更改我的xaml,以使红色X出现。(ps。灰色背景是由于我资源中的按钮样式)
我使用路径几何组来构建替换WPF中的Button的几何:
<UserControl.Resources>
<ControlTemplate x:Key="bT" TargetType="Button">
<Path StrokeThickness="1.5" >
<Path.Data>
<GeometryGroup>
<EllipseGeometry Center="7,7" RadiusX="7" RadiusY="7" />
<LineGeometry StartPoint="4,4" EndPoint="10,10" />
<LineGeometry StartPoint="10,4" EndPoint="4,10" />
</GeometryGroup>
</Path.Data>
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Stroke" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</ControlTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
该按钮的实现方式如下
<Button Template="{StaticResource ResourceKey=bT}" />
Run Code Online (Sandbox Code Playgroud)
现在我的问题是我不能用不同的颜色设置几何形状的样式。例如,我希望EllipseGeometry的行为不同于LineGeometries的行为。有没有办法做到这一点?
我有一个 WPF 问题DatePicker。
DatePicker表单上有两个控件,基于StaticResource DateTheme. 的DateTheme样子:
<ResourceDictionary>
<Style TargetType="{x:Type DatePicker}" x:Key="DateTheme" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePicker}">
<DatePicker DisplayDate="{TemplateBinding Property=DisplayDate}" SelectedDate="{TemplateBinding Property=SelectedDate}" Background="#353340" Foreground="Black"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
该DatePicker只是样子:
<DatePicker x:Name="dtpFrom" Style="{StaticResource DateTheme}" />
<DatePicker x:Name="dtpTo" Style="{StaticResource DateTheme}" />
Run Code Online (Sandbox Code Playgroud)
并且DateTheme包含在 app.xaml 中:
<ResourceDictionary Source="DateTheme.xaml" />
Run Code Online (Sandbox Code Playgroud)
但是在选择新日期并单击按钮后,代码中仍使用旧日期。SelectedDate不在控件中更新。
这里SelectedDate仍然是旧日期,而新选择的日期显示在屏幕上:
if (dtpFrom.SelectedDate > dtpTo.SelectedDate)
Run Code Online (Sandbox Code Playgroud)
SelectedDate在标准DateTimePicker控件中选择新日期时如何实际更改?
wpf ×6
xaml ×4
binding ×2
button ×2
c# ×2
.net ×1
converter ×1
datatemplate ×1
geometry ×1
listbox ×1
parent ×1
silverlight ×1
storyboard ×1
triggers ×1