简体   繁体   English

TextBlock宽度绑定到GridView

[英]TextBlock width binding to GridView

On my XAML page I have a text block with following binding: 在我的XAML页面上,我有一个具有以下绑定的文本块:

<TextBlock Width="{Binding ActualWidth, ElementName=SessionList, Mode=OneWay}" ...  />

This binds to a grid view: 这绑定到网格视图:

<GridView x:Name="SessionList" ItemsSource="{Binding Sessions}"... />

Now when the page first loads and data is available, the text block will be visible and have the correct width. 现在,当页面首次加载且数据可用时,该文本块将可见并且具有正确的宽度。 When the page loads and there is no data, the text box will not be visible because of the bound width. 当页面加载并且没有数据时,由于绑定的宽度,文本框将不可见。

But ... when I load up data in the background and after a while the data comes in (through MVVM) the list will be show, but the text block width will not change accordingly, and setting it as TwoWay has no effect. 但是...当我在后台加载数据并且过一会儿(通过MVVM)输入数据时,将显示该列表,但文本块的宽度不会相应改变,将其设置为TwoWay无效。

Any ideas/tips? 有什么想法/提示吗?

ActualWidth is not a property that you can bind to within WinRT. ActualWidth不是可以在WinRT中绑定的属性。 Not sure if you are showing static text or bound text. 不确定是显示静态文本还是绑定文本。 If bound text and data is same as GridView has then it should go away if data is null. 如果绑定的文本和数据与GridView相同,则如果data为null,则它应该消失。 If static data, then use a ValueConverter to set the visibility of the TextBlock based on the data being null/empty 如果是静态数据,则使用ValueConverter基于数据为null / empty设置TextBlock的可见性

Binding issues like this are usually caused by properties that are not bindable, ie they are not dependency properties and/or do not implement INotifyPropertyChanged. 诸如此类的绑定问题通常是由不可绑定的属性引起的,即它们不是依赖项属性和/或未实现INotifyPropertyChanged。 Whatever. 随你。 I use a Attached Dependency Property or, if that does not cover enough, a behavior. 我使用附加依赖项属性,或者,如果使用的不是足够的话,则使用行为。 Now behavior are not included in WinRT, but that problem has already been addressed ;-) 现在,行为不包括在WinRT中,但是该问题已经得到解决 ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM