[UWP]以编程方式获取/设置ListBox TextBlock属性 [英] [UWP]Programmatically Get/Set ListBox TextBlock properties

查看:56
本文介绍了[UWP]以编程方式获取/设置ListBox TextBlock属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要  以编程方式获取/设置ListBox TextBlock属性...特别是FontSize属性。 或者,或者某种方式让TextBlock自动调整其大小以适应屏幕,当设备从横向旋转到纵向时为
。 但是,编译器不允许我直接访问ListBox.TextBlock ...只有ListBox控件,我已经没有想法。 这是XAML:

I need to Programmatically Get/Set a ListBox TextBlock properties...specifically the FontSize property.  Or, alternatively some way for the TextBlock to automatically resize it's Font to fit the screen when the device is rotated from Landscape to Portrait.  But, the compiler won't allow me to access the ListBox.TextBlock directly...only the ListBox control, and I've run out of ideas.  Here is the XAML:

<ListBox x:Name="thisListBox">
	<ListBox.ItemTemplate>
		<DataTemplate>
			<TextBlock Name="thisListBoxTextBlock" Text="{Binding}"/>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>


//I've tried modifying the FontSize property of the ListBox:
thisListBox.FontSize = 10;
//But, it doesn't effect the TextBlock

//When I display the following in the immediate window I get the TextBlock object and can see the FontSize value:
this.thisListBox.ItemTemplate.LoadContent()

//So, I tried accessing the GetValue method:
DependencyProperty dependencyProperty = DependencyProperty.Register("FontSize", typeof(double), typeof(TextBlock), null);
Windows.UI.Xaml.DataTemplate dataTemplate = this.thisListBox.ItemTemplate;
object thisObject = dataTemplate.LoadContent().GetValue(dependencyProperty);
//but, it only returns 0.

推荐答案

嗨史蒂文,

你无法得到的名字如果它在< DataTemplate>内直接控制。

You can not get the name of the control directly if it is inside the <DataTemplate>.

您是否尝试使用绑定到FontSize等属性然后更改后面代码中的值?

Have you tried to use binding to the properties like FontSize then change the value in code behind?

问候,

Stanly


这篇关于[UWP]以编程方式获取/设置ListBox TextBlock属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆