【发布时间】:2014-01-10 10:30:26
【问题描述】:
我正在使用下一个代码来显示 ListView。 ListView 包含一个 TextBlock,它的 FontSized 绑定到我的 MainPage 上的一个变量:
<ListView x:Name="ListView"
SelectionMode="Single"
SelectionChanged="ListView_OnSelectionChanged"
Grid.Row="1"
Margin="8,16"
>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding DisplayName}"
FontSize="{Binding Path=FontSizeListViewTitle}" Margin="6,0,0,0" TextWrapping="NoWrap" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
问题在于 FontSize="{Binding Path=FontSizeListViewTitle}" 似乎无法正常工作。尽管变量 FontSizeListViewTitle 仅采用 16 到 24 之间的值,但它向我显示了 fontSize 接近 6(或其他值)的文本。
有趣的是,如果我将 TextBlock 放在 ListView 之外,它可以完美运行。仅当我尝试在 ListView 中使用绑定时才会出现问题。 此外,Text="{Binding DisplayName} 完美运行,TextBlock 显示它必须显示的文本。
现在来自 .cs 文件的一些代码:
private int _fontSizeListViewTitle;
public int FontSizeListViewTitle
{
get { return _fontSizeListViewTitle; }
set
{
_fontSizeListViewTitle = value;
OnPropertyChanged("FontSizeListViewTitle");
}
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
public MainPage()
{
InitializeComponent();
this.DataContext = this;
ListView.ItemsSource = MyList; // MyList is an Observable Collection
}
【问题讨论】:
标签: c# xaml listview data-binding microsoft-metro