我只是在使用Silverlight中的自定义控件,而对于我的生活,我无法使TemplateBindings工作.有人可以给这个缩小版本一次,看看我是否遗漏了什么.
所以我在generic.xaml中的ControlTemplate看起来像
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NumericStepperControl;assembly=NumericStepperControl">
<Style TargetType="local:NumericStepper">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NumericStepper">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Black" BorderThickness="2" Width="50" Height="30">
<TextBlock Width="50" Height="30" Text="{TemplateBinding Value}" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
我的控件类看起来像:
namespace NumericStepperControl
{
public class NumericStepper : Control
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(NumericStepper), new PropertyMetadata(20));
public NumericStepper()
: base()
{
DefaultStyleKey = typeof( NumericStepper );
}
public int Value
{
get
{
return (int)GetValue(ValueProperty);
} …Run Code Online (Sandbox Code Playgroud) WPF的新功能并且具有选项卡,并且在每个选项卡中,内容以弯曲的角落面板/窗口/ whateveryouwannacallit呈现.我不确定如何做到这一点(Style,ControlTemplate)但决定采用DataTemplate方式.
所以现在我有了这个DataTemplate:
<DataTemplate x:Key="TabContentPresenter" >
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Padding="5"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
正如你可以看到的背景属性,我不想在内容中设置背景颜色,但不知道如何.我在这里使用它.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Background="White">
<!-- Something Here -->
</ContentControl>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Grid.Row="1" Background="Blue">
<!-- Something Here -->
</ContentControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在这里使用DataTemplate是错误还是有其他方法吗?
我可以直接在内容上设置背景,并从模板中的填充更改为内容中的边距,但在一些类似的情况下无法工作,只需设置一次就更好了.
编辑:
根据建议,我改为ControlTemplate并将其置于样式中.这解决了背景问题,但创造了一个更大的问题.现在内容不会出现.我在博客上看到,放一个targetType解决了这个问题,但它并没有解决我的问题.代码现在看起来像这样,并且还改变了ContentControl以使用样式而不是模板.
<Style x:Key="TabContentPresenter" TargetType="ContentControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</ControlTemplate>
</Setter.Value> …Run Code Online (Sandbox Code Playgroud) 我已经成功创建了一个文本框,根据模型/ vm中设置的验证规则显示/折叠错误消息.对于电子邮件,代码如下:
<StackPanel Grid.Row="3" Grid.Column="1">
<TextBox MaxLength="200" x:Name="mailTextBox"
Style="{StaticResource SectionEditPropertyTextBox}"
Text="{Binding Email, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<ContentPresenter Visibility="{Binding ElementName=mailTextBox, Path=(Validation.HasError), Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=True }"
Content="{Binding ElementName=mailTextBox, Path=(Validation.Errors).CurrentItem}"
HorizontalAlignment="Left">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Label Style="{StaticResource SectionEditErrorLabel}" Content="{Binding Path=ErrorContent}"/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
由于我有很多这些,我本来希望将所有这些放在一个控制模板中,并将其重新定位在一个公共资源文件中.
我的模板看起来像这样:
<ControlTemplate x:Key="FormTextBox" TargetType="{x:Type TextBox}">
<StackPanel Grid.Row="{TemplateBinding Grid.Row}" Grid.Column="{TemplateBinding Grid.Column}">
<TextBox x:Name="validableText" MaxLength="{TemplateBinding MaxLength}"
Style="{StaticResource SectionEditPropertyTextBox}"
Text="{TemplateBinding Text}" />
<ContentPresenter Visibility="{Binding ElementName=validableText, Path=(Validation.HasError), Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=True }"
Content="{Binding ElementName=validableText, Path=(Validation.Errors).CurrentItem}"
HorizontalAlignment="Left">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Label Style="{StaticResource SectionEditErrorLabel}" Content="{Binding Path=ErrorContent}"/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter> …Run Code Online (Sandbox Code Playgroud) 在资源字典中:
<Style x:Key="ControlFrame" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border Background="{TemplateBinding Background}" BorderThickness="2">
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle StrokeDashArray="8, 2" Stroke="{TemplateBinding BorderBrush}"
StrokeThickness="2"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"/>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
在C#中:
TextBox textbox = new TextBox();
textbox.Width = 200;
textbox.Height = 200;
Style style = this.FindResource("ControlFrame") as Style;
textbox.Style = style;
canvas.Children.Insert(0, textbox);
Run Code Online (Sandbox Code Playgroud)
我可以正确地获得虚线边框.

如果我包裹文本框成ContentControl中而不给予的高度和宽度文本框如下所示:
TextBox textbox = new TextBox();
Style style = this.FindResource("ControlFrame") …Run Code Online (Sandbox Code Playgroud) 假设我有以下getter/setter方法
get next() {
console.log(this.people[this._index], this._index);
return this.people[this._index];
}
set next(i: any) {
this._index = (+i) + 1;
this._index = (+i) % this.people.length;
}Run Code Online (Sandbox Code Playgroud)
我想用以下方式调用它:
<ng-template ngFor let-person="$implicit" [ngForOf]="people" let-i=index let-last=last>
<app-card [cardItem]="people[i]" [nextCard]="next(i)"></app-card>
</ng-template>Run Code Online (Sandbox Code Playgroud)
PS:把它想象成圆形阵列.我在哪里需要prev,current和next items.
但是我得到以下错误
Angular:会员'下一个'不可赎回
这是为什么?最新解决方案是什么?
谢谢
谢谢你们的帮助和解释.在你的帮助下,我设法让它发挥作用:
<app-card [currentCard]="people[i]" [nextCard]="people[i === people.length - 1 ? 0: i + 1]" [prevCard]="i == 0 ? people[people.length - 1] : people[i - 1]"></app-card>Run Code Online (Sandbox Code Playgroud)
所以它几乎是圆形阵列.让我们假设我们有以下内容:
people["James Dan", "Aluan Haddad", "Jota Toledo"]
条件很少:
index = 0) - …我们创建了一个自定义 ComboBox 控件,它有一个按钮来清除 ComboBox 的选择:
<Style TargetType="{x:Type local:ClearableComboBox}">
<Setter Property="SelectedItem" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ClearableComboBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<Button Name="btnClear" DockPanel.Dock="Right" ToolTip="Clear" Width="20">
<Image Source="pack://application:,,,/img/icons/silk/cross.png" Stretch="None" />
</Button>
<ComboBox Name="comboBox"
ItemsSource="{TemplateBinding ItemsSource}"
SelectedItem="{TemplateBinding SelectedItem}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}" />
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
ItemsSource 的绑定工作正常,但是 SelectedItem 的绑定没有。在谷歌搜索后,我在这里找到了问题的解决方案。具体来说,将 SelectedItem 绑定更改为
SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedItem}"
使其按预期工作。
为什么 SelectedItem 上的原始 TemplateBinding 不起作用,而 ItemsSource 的 TemplateBinding 工作得很好?
我为选项卡控件制作了一个模板.它的作用是在鼠标悬停时采用边框和动画的背景.
当鼠标离开时,它应该从Background属性中读取当前值并相应地设置它们.
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="false"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Bd"
Storyboard.TargetProperty="Background.GradientStops[0].Color" To="#003372"
Duration="0:0:0.2"/>
<ColorAnimation Storyboard.TargetName="Bd"
Storyboard.TargetProperty="Background.GradientStops[1].Color" To="#025092"
Duration="0:0:0.2"/>
<ColorAnimation Storyboard.TargetName="Bd"
Storyboard.TargetProperty="Background.GradientStops[2].Color" To="#003372"
Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Bd"
Storyboard.TargetProperty="Background.GradientStops[0].Color" To="{TemplateBinding Background.GradientStops[0].Color}"
Duration="0:0:0.2"/>
<ColorAnimation Storyboard.TargetName="Bd"
Storyboard.TargetProperty="Background.GradientStops[1].Color" To="{TemplateBinding Background.GradientStops[1].Color}"
Duration="0:0:0.2"/>
<ColorAnimation Storyboard.TargetName="Bd"
Storyboard.TargetProperty="Background.GradientStops[2].Color" To="{TemplateBinding Background.GradientStops[2].Color}"
Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
Run Code Online (Sandbox Code Playgroud)
问题是
To="{TemplateBinding Background.GradientStops[0].Color}"部分不起作用.我应该在那里写什么?
在我的 WPF ListBox 中,我有一个带有 ListBoxItem 的 ControlTemplate 的样式。在那个 ControlTemplate 里面,我定义了一个标签。根据一些细节,我需要更改标签的字体大小。所以从我的代码隐藏中,我需要确定字体应该是什么,然后我需要设置它。
这是我使用 ControlTemplate 的风格(我去掉了一些不相关的控件)
<Style x:Key="RecordTabList" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="{DynamicResource RecordIndexTabBackcolor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Label
x:Name="myLabel" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1" Margin="3,-2,0,-2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="{DynamicResource RecordIndexTabForeground}"
FontSize="10" Height="Auto" BorderThickness="3,0,0,0"
Content="{Binding Path=Name}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有一个带有DataTemplate的ListView,用于为每个ListViewItem显示一个Checkbox.

<ListView ItemsSource="{Binding TableNames}">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected, Mode=TwoWay}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
ItemsSource("TableNames")在ViewModel中被解除,如下所示:
private ObservableCollection<Item> _TableNames = new ObservableCollection<Item>();
public ObservableCollection<Item> TableNames
{
get { return _TableNames; }
set
{
_TableNames = value;
OnPropertyChanged("TableNames");
}
}
public class Item
{
public bool IsSelected { get; set; }
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
Run Code Online (Sandbox Code Playgroud)
如何将Checkbox中的IsChecked绑定到Item.IsSelected属性?
我的代码不起作用.
我正在尝试在Silverlight中创建一个自定义控件,动态缩放其ControlTemplate中的元素.ControlTemplate的第一次尝试看起来像这样:
<ControlTemplate TargetType="controls:ProgressBar">
<Grid>
<Rectangle x:Name="TrackPart" Fill="{TemplateBinding Background}" HorizontalAlignment="Left" />
<Rectangle x:Name="ProgressPart" Fill="Blue" >
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="{TemplateBinding Progress}" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
但是,此论坛帖子声明TemplateBinding仅适用于FrameworkElements的衍生物.ScaleTransform不是FrameworkElement.有没有解决这个问题?针对这种情况的最佳做法是什么?
templatebinding ×10
wpf ×7
binding ×3
c# ×3
xaml ×2
angular ×1
animation ×1
checkbox ×1
datatemplate ×1
listview ×1
silverlight ×1
templates ×1
textbox ×1
typescript ×1