【发布时间】:2015-09-15 17:11:47
【问题描述】:
所以我有一个浮点数组,我想将它作为 ListBox 中的 ItemSource。
在 ItemTemplate 里面我有一个进度条,它应该将它的 Progress 值绑定到给定的浮点值。但是我永远看不到这些值实际上绑定到 Progress 属性。
xaml 代码(我不知道我是否错了,但我预计会有从 float 到 double 的隐式转换):
<ListBox ItemsSource="{Binding CoreLoads, Mode=OneWay}" BorderThickness="0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type sys:Double}">
<StackPanel>
<ctrl:MetroProgressBar Orientation="Vertical" Progress="{Binding}" ExtenedBorderWidth="0.2" Width="30" Height="50" VerticalAlignment="Center"
HorizontalAlignment="Center" BorderBrush="Black" BorderThickness="2" Background="White" Margin="5"/>
<TextBlock Margin="0,3,0,3" HorizontalAlignment="Center" Text="{Binding LastUpdateTime, StringFormat='{}{0:hh:mm:ss tt}', Mode=OneWay}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
xmlns:sys="clr-namespace:System;assembly=mscorlib"
以及房产本身:
public double[] CoreLoads
{
get { return cpuManagement.ProcessorInfo.LoadPercentages; }
}
注意:我使用的进度条是自定义控件,继承自System.Windows.Controls.Control。
问题似乎是数组的值没问题,但是当绑定到 TextBlock 时,值是 0,所以进度条的进度总是 0。所以,我是否有正确的数据模板双数组?还是我应该换成另一种类型的收藏?
【问题讨论】:
-
尝试将数据绑定到
ProgressBar.ValueProperty。 -
@Sheridan 我应该提到我正在使用的进度条是一个继承
System.Windows.Constrols.Constrol的自定义控件。所以没有价值属性(在这种情况下可以说ProgressBar.Value=MetroProgressBar.Progress。) -
那么,您想将进度条的值绑定到您的 ListBox 的选定值吗?
-
@floyd 是的,这正是我想要做的。但我不知道我的方法是否正确,因为您通常会为模板提供数据类型并绑定到其成员(?)(我对 wpf 很陌生)。
标签: arrays wpf xaml data-binding listbox