WPF,这种数据绑定如何写
WPF,这种数据绑定怎么写?
一个自定义控件:;
自定义控件CustomControl1,有一个自定义的依赖项属性Value,其值要绑定到控件模板中的PART_TextBlock1的Text上,在设置绑定的时候,该怎么写呢,用什么绑定呢?ElementName似乎不行吧,没有在同一名称范围吧
------解决方案--------------------
试一下用TemplateBinding
------解决方案--------------------
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Value" Value=""/> //这里不用写,删掉就可以。但确保.cs文件定义了Value的依赖属性 <Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Style="{StaticResource border1}" Height="160" Width="160">
<TextBlock Name="PART_TextBlock1" Text="张三" /> //张三改为一楼所写TemplateBinding Value </Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
------解决方案--------------------
首先 确保你的 CustomControl1 支持Value属性,再使用TemplateBinding
------解决方案--------------------
都认真读题了,你的这个要求就是用模板绑定就行了,<TextBlock Name="PART_TextBlock1" Text="{TemplateBinding Value}" />
一个自定义控件:;
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Value" Value=""/> //这里怎么写
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Style="{StaticResource border1}" Height="160" Width="160">
<TextBlock Name="PART_TextBlock1" Text="张三" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
自定义控件CustomControl1,有一个自定义的依赖项属性Value,其值要绑定到控件模板中的PART_TextBlock1的Text上,在设置绑定的时候,该怎么写呢,用什么绑定呢?ElementName似乎不行吧,没有在同一名称范围吧
------解决方案--------------------
试一下用TemplateBinding
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Value" Value=""/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Style="{StaticResource border1}" Height="160" Width="160">
<TextBlock Name="PART_TextBlock1" Text="{TemplateBinding Value}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
------解决方案--------------------
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Value" Value=""/> //这里不用写,删掉就可以。但确保.cs文件定义了Value的依赖属性 <Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Style="{StaticResource border1}" Height="160" Width="160">
<TextBlock Name="PART_TextBlock1" Text="张三" /> //张三改为一楼所写TemplateBinding Value </Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
------解决方案--------------------
首先 确保你的 CustomControl1 支持Value属性,再使用TemplateBinding
------解决方案--------------------
都认真读题了,你的这个要求就是用模板绑定就行了,<TextBlock Name="PART_TextBlock1" Text="{TemplateBinding Value}" />