Nacho
Nacho

Reputation: 99

WPF, bind specific item of a List to a TextBlock

I have a List of doubles. I want to bind a specific item of this list to a TextBlock. This specific item is determined by another control (ComboBox):

<ComboBox Name="MyBox">
....
</ComboBox>

<TextBlock Binding="{MyList, >get item index == MyBox.SelectedIndex< }"/>

The solution I have in place right now, is to bind the TextBlock to another property of my context, however, I would prefer the other way as this forces me to have several Property Changed Notifications in place...

Thanks.

Upvotes: 0

Views: 1345

Answers (2)

Nacho
Nacho

Reputation: 99

I came up with a solution that fits my needs, it involves using MultiBinding and a Converter:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource GenericMemConverter}">
            <Binding Path="TotalGPUMemory"/>
            <Binding ElementName="CurPlatformView" Path="SelectedIndex"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

As you can see, I'm pointing to another control of the app and passing SelectedIndex.

Converter:

    public object Convert(object[] o, Type type, object parameter, CultureInfo culture)
    {
        List<long> vals = (List<long>)o[0];
        int plat = (int)o[1];

        double mb = (double)vals[plat] / 1024.0 / 1024.0;
        return mb.ToString("N1") + "MB";
    }

Upvotes: 0

Alfie
Alfie

Reputation: 2013

You can reference another control in the .xaml using ElementName=_ within the binding, then specify the path of the binding using Path=_, like so:

<ComboBox Name="MyBox">
....
</ComboBox>

<TextBlock Binding="{Binding ElementName=MyBox, Path=SelectedItem}"/>

Upvotes: 2

Related Questions