当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to bind listview selected item to other controls?

问题描述:

Consider i have a collection with 10 items. the list view is binded to this collection. therefore 10 items in list view will appear.

Listview selection mode is set to single item. Only one item can be selected at a time.

Listview has 3 columns. Frequency, Amplitude and Phase. each of these properties should be controlled by the tools say slider. so when the user drags slider the properties of the selected item should be changed.

So i want to bind selected item properties to some sliders. I was able to do this in code behind. it seems ugly way i was wondering if it is possible in XAML.

The simplified model is like this. (only one property is shown)

internal class Waveform : INotifyPropertyChanged

{

public double Frequency { get; set; }

private void Initialize()

{

FreqBinding = new Binding

{

Source = this,

Path = FreqPath,

Mode = BindingMode.TwoWay

};

}

public Binding FreqBinding { get; private set; } // binding used to bind this item to frequency slider

private static readonly PropertyPath FreqPath = new PropertyPath(nameof(Frequency));

}

Here is one of the sliders.

<Slider x:Name="FreqSlider"/>

There is event that fires when list view selected index changes.

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

Toolbox.IsEnabled = ListView.SelectedIndex >= 0;

if (Toolbox.IsEnabled)

{

ResetBindings((Waveform)ListView.SelectedItem);

}

else

{

BindingOperations.ClearAllBindings(FreqSlider);

}

}

private void ResetBindings(Waveform obj)

{

FreqSlider.SetBinding(RangeBase.ValueProperty, obj.FreqBinding);

}

This is done for all 3 controls in toolbox. i want to know if it is possible to do this all in XAML?

To make it a little more clear. here is the current XAML code i have. simplified.

<ListView x:Name="ListView" ItemsSource="{Binding WaveCollection, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" SelectionMode="Single">

<ListView.View>

<GridView>

<GridViewColumn Header="Frequency" DisplayMemberBinding="{Binding Frequency, StringFormat=0.#### Hz}"/>

</GridView>

</ListView.View>

</ListView>

Now looking for a way to do this binding.

<Slider x:Name="FreqSlider" Value="{Binding ???}"/>

网友答案:

If WaveCollection is the ObservableCollection<WaveForm>. And if WaveForm class properties like Frequency looks like this (with NotifyPropertyChanged realization).

private double _freq;

public double Frequency
{
    get { return _freq; }
    set { _freq = value; NotifyPropertyChanged( "Frequency" ); }
}

#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
protected void NotifyPropertyChanged( String info )
{
    if ( PropertyChanged != null )
    {
        PropertyChanged( this, new PropertyChangedEventArgs( info ) );
    }
}

You can write Binding for sliders like this.

{Binding ElementName=ListView, Path=SelectedItem.Frequency}

Update or as mentioned in comments

{Binding ElementName=ListView, Path=SelectedItem.(local:Waveform.Frequency)}
分享给朋友:
您可能感兴趣的文章:
随机阅读: