0
votes

So i have a float array which i want to have as ItemSource in a ListBox.
Inside the ItemTemplate i have a progress bar, that should bind its Progress value to the given float value. Yet i can't ever see that the values are actually bound to the Progress property.

The xaml code (i don't know whether i'm wrong but i expected that there's a implicit cast from float to 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"

and the property itself:

public double[] CoreLoads
{
    get { return cpuManagement.ProcessorInfo.LoadPercentages; }
}

Note: The progress bar i'm using is a custom control and inherits from System.Windows.Controls.Control.

Problem seems to be that the values of the array are ok but when bound to a TextBlock the values are 0, so the progress of the progress bar is always 0. So, am i having a correct data template for a double array? Or should i change to another type of collection?

2
Try data binding to the ProgressBar.Value Property. - Sheridan
@Sheridan I should have mentioned that the progress bar i'm using is a custom control that inherits System.Windows.Constrols.Constrol. So there is no value property (in this case one could say ProgressBar.Value = MetroProgressBar.Progress.) - Streamline
So, you want to bind a value of the progress bar to the selected value of your ListBox? - floyd
@floyd Yes, that is exactly what i want to do. But i don't know whether my approach is correct since you would normally provide a data type for the template and bind to its members (?) (i'm pretty new to wpf). - Streamline

2 Answers

0
votes

I guess you should create a property in a ViewModel(assuming you're using MVVM pattern) which will represent selected value of the ListBox:

    private double selectedCoreLoad;
    public Double SelectedCoreLoad
    {
        get
        {
            return selectedCoreLoad;
        }
        set
        {
            if (selectedCoreLoad != value)
            {
                selectedCoreLoad = value;
                RaisePropertyChanged("SelectedCoreLoad");
            }
        }
    }

Then, you should bind selected value of the ListBox to this property:

<ListBox ItemsSource="{Binding CoreLoads, Mode=OneWay}" SelectedValue="{Binding SelectedCoreLoad, Mode=TwoWay}" BorderThickness="0">

<ctrl:MetroProgressBar Orientation="Vertical" Progress="{Binding SelectedCoreLoad}" ExtenedBorderWidth="0.2" Width="30" Height="50" VerticalAlignment="Center"
                                       HorizontalAlignment="Center" BorderBrush="Black" BorderThickness="2" Background="White" Margin="5"/>

UPD

Use ObservableCollection instead:

private ObservableCollection<Double> coreLoads;
public ObservableCollection<Double> CoreLoads
{
    get { return coreLoads; }
    set
    {
        coreLoads = value;
        RaisePropertyChanged("CoreLoads");
    }
}
0
votes

So found the answer: ListBox seems not to like arrays as ItemsSource. After changing the source to a double-list everything works.

public List<double> CoreLoads
{
    get { return new List<double>(cpuManagement.ProcessorInfo.LoadPercentages); }
}