我无法弄清楚为什么这个XAML代码不起作用.使用TemplateBinding(见下文)时,未设置背景颜色.但是当我使用普通的颜色字符串(即"红色")时,它工作正常.
<ControlTemplate x:Key="InstanceButtonTemplate" TargetType="{x:Type Control}">
<Grid>
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="{TemplateBinding Background}"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
然而,当我以这种方式使用TemplateBinding时,它工作正常......
<ControlTemplate x:Key="InstanceButtonTemplate" TargetType="{x:Type Control}">
<Grid>
<Rectangle Fill="{TemplateBinding Background}"></Rectangle>
</Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑:为了澄清,我打算将其扩展为使用渐变画笔,这就是为什么我需要能够使用XAML而不是普通字符串分配给Rectangle.Fill属性.
我正在尝试理解本机knockoutjs 模板绑定,尤其是foreach绑定.
只是想知道如何使用原生出价访问当前项目?有jQuery.tmpl可能使用像$item/ 这样的东西$data.当数据源是基元数组时,如何使用本机模板绑定执行相同的操作,因此每个项目都没有命名字段?这是JSFiddle的两个例子,第一个 - 使用本机绑定,其中数据源是自定义对象的数组,第二个 - 绑定到字符串数组.我无法让它工作,看起来我错过了一些明显的东西?
基本上我正在尝试理解本机绑定并且能够使用本机绑定重构以下示例:JSFiddle:逗号分隔的已检查项目列表,因此我可以保留一个可观察变量,该变量重新命名以逗号分隔的已检查项目列表.
所以我有按钮及其样式:
\n<Style TargetType="Button" x:Key="BaseButtonStyle">\n <Setter Property="FontWeight" Value="Bold" />\n <Setter Property="Width" Value="120" />\n <Setter Property="Height" Value="30" />\n <Setter Property="BorderBrush" Value="#1FA3FF" />\n</Style>\n\n<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle" BasedOn="{StaticResource BaseButtonStyle}"> \n <Setter Property="Background" Value="#1FA3EB" />\n <Setter Property="Foreground" Value="#FFFFFF" />\n</Style>\n\n<Button Style="{StaticResource MyButtonStyle}" Content="My text" />\nRun Code Online (Sandbox Code Playgroud)\n它工作正常,我得到以下结果(海蓝宝石背景不是按钮的一部分,它属于窗口):
\n
但后来我想替换按钮的内容并更改我的样式:
\n<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle" BasedOn="{StaticResource BaseButtonStyle}"> \n <Setter Property="Background" Value="#1FA3EB" />\n <Setter Property="Foreground" Value="#FFFFFF" />\n\n <Setter Property="Template">\n <Setter.Value>\n <ControlTemplate>\n <StackPanel Orientation="Horizontal">\n <TextBlock Text="\xe2\x88\x9a" />\n <TextBlock Text="{TemplateBinding Button.Content}" />\n </StackPanel>\n </ControlTemplate>\n </Setter.Value>\n </Setter>\n</Style>\nRun Code Online (Sandbox Code Playgroud)\n现在我的按钮变得很糟糕: …
当我在带有返回我的数据的函数的angular中使用* ngFor时,该函数被多次调用,有时甚至会导致循环:
app.component.ts
export class AppComponent {
getArray(): string[] {
//here i know when this function is called
console.log('getArray called')
return ['number one', 'number two']
}
}
Run Code Online (Sandbox Code Playgroud)
app.component.html
<h1 *ngFor="let item of getArray()">
{{ item }}
</h1>
Run Code Online (Sandbox Code Playgroud)
我的控制台:
然后我得到了多次调用函数getArray(),我不知道为什么。
wpf ×2
angular ×1
binding ×1
c# ×1
knockout-2.0 ×1
knockout.js ×1
mvvm ×1
ngfor ×1
typescript ×1
xaml ×1