Can I change property of element in DataTemplate ? For example:
我可以在DataTemplate中更改元素的属性吗?例如:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="LawItemTemplate">
<StackPanel>
<TextBlock
Text="{Binding Name}"
TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
I want to change TextBlock FontSize, but How I can get access to TextBock in DataTemplate?
我想更改TextBlock FontSize,但是如何在DataTemplate中访问TextBock?
Option to create Load Event and get an element by sender does not fit, because the changes visually noticeable to the user. Thanks.
创建Load Event并通过发件人获取元素的选项不适合,因为这些更改在视觉上对用户来说是显而易见的。谢谢。
3 个解决方案
#1
0
If you want to change something in a DataTemplate
just bind it to a property on your data and change that instead. Messing with the controls should be avoided if possible.
如果要在DataTemplate中更改某些内容,只需将其绑定到数据上的属性并进行更改即可。如果可能,应避免与控件混淆。
#2
0
First of all get the root element of ListBox
item
首先获取ListBox项的根元素
ListBoxItem item = listbox.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
Then search TextBlock
in elements tree with several calls to .Children()
or give a Name
to TextBlock
and search by name
然后在元素树中搜索TextBlock,多次调用.Children()或者给TextBlock命名并按名称搜索
#3
0
I'm not sure if that is what you mean, but you can use Trigger
s.
我不确定这是不是你的意思,但你可以使用触发器。
Like so:
像这样:
<DataTemplate x:Key="LawItemTemplate">
<StackPanel>
<TextBlock x:Name="NameHolder"
Text="{Binding Name}"
TextWrapping="Wrap"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SomeProp}" Value="SomeValue">
<Setter TargetName="NameHolder" Property="FontSize" Value="18"/>
</DataTrigger>
</DataTemplate.Triggers>
</StackPanel>
</DataTemplate>
Of course, you can use RelativeSource
in the binding of the trigger if you need to listen to property changes of a visual ancestor.
当然,如果需要监听可视祖先的属性更改,可以在触发器的绑定中使用RelativeSource。
#1
0
If you want to change something in a DataTemplate
just bind it to a property on your data and change that instead. Messing with the controls should be avoided if possible.
如果要在DataTemplate中更改某些内容,只需将其绑定到数据上的属性并进行更改即可。如果可能,应避免与控件混淆。
#2
0
First of all get the root element of ListBox
item
首先获取ListBox项的根元素
ListBoxItem item = listbox.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
Then search TextBlock
in elements tree with several calls to .Children()
or give a Name
to TextBlock
and search by name
然后在元素树中搜索TextBlock,多次调用.Children()或者给TextBlock命名并按名称搜索
#3
0
I'm not sure if that is what you mean, but you can use Trigger
s.
我不确定这是不是你的意思,但你可以使用触发器。
Like so:
像这样:
<DataTemplate x:Key="LawItemTemplate">
<StackPanel>
<TextBlock x:Name="NameHolder"
Text="{Binding Name}"
TextWrapping="Wrap"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SomeProp}" Value="SomeValue">
<Setter TargetName="NameHolder" Property="FontSize" Value="18"/>
</DataTrigger>
</DataTemplate.Triggers>
</StackPanel>
</DataTemplate>
Of course, you can use RelativeSource
in the binding of the trigger if you need to listen to property changes of a visual ancestor.
当然,如果需要监听可视祖先的属性更改,可以在触发器的绑定中使用RelativeSource。