Perter

I have a class containing a field which is an array of 23 int:

class myClass

{

private int[] m_myValues;

public int[] myValues

{

set{m_myValues = value;}

get{return m_myValues;}

}

}

Although the array is one field in the class, I would like the UI to have a slider and textbox for each int value of the array. I would like to create a data template in XAML to respond to these values changing (two way binding). I am having a hard time determining how to do this. Something like:

<DataTemplate x:Key ="ColumnTemplate">

<Slider x:Name="sld1_63" Tag="1" Width="Auto" Orientation="Vertical" Maximum="100" Minimum="0" Value="{Binding Value, Mode=TwoWay}" Height="118" ToolTip="63" />

<TextBox Text="{Binding Value, Mode=TwoWay}" x:Name="numEQ1_63" Width="Auto" Max="100" HorizontalAlignment="Stretch" d:LayoutOverrides="Height" VerticalAlignment="Stretch" />

</DataTemplate>

I'm getting a little lost from here...any help is appreciated



Re: Windows Presentation Foundation (WPF) How to: data template int[]?

Marlon Grech

Hi there,

I am not sure waht you want to do but in order to bind to a collection and receive notifications you need to use the ObservableCollection<T> because this collection implements the INotifyCollectionChanged that notifies the UI that the collection has changed..


Again I did not understand 100% what you need to do... maybe if you post all your code this would be more clear...





Re: Windows Presentation Foundation (WPF) How to: data template int[]?

Perter

(disclaimer: I am new to WPF!)

Some background: I am using classes from an existing framework built in .NET 2.0 or lower. The class I have above simplifies what I have to work with; I have a field which is an array of 23 integers (Representing a 1/3 octave equalizer having 23 bands). My goal is to represent these values to the user with 23 slider controls which they can use to update the values. Each slider also has a TextBox below it where a user could type the number rather than use the slider.

Previously I had just placed 23 sliders and 23 textboxes into a grid with 23 columns and bound each slider value to it's matching textbox. In the code behind I would manually iterate through the array and assign each value to the appropriate slider. Although this was fairly repsonsive for the user, I was hoping to come up with a more elegant solution. So I really have no other code yet; just the DataTemplate above that I was guessing at but was stumped with how to bind an array of ints. I suppose I might be able to wrap the int[] with an ObservableCollection and go from there.

My previous UI worked pretty well until I added the next feature; a separate 'preset' slider that would automatically change all 23 sliders simultaneously to preset values as you move the preset slider. Performance of the UI really lagged from all of the code behind going on - I also thought binding everything to the field might make it snappier.

Anyways I'll lookup ObservableCollection...if you have any other input I'll certainly take it. Thanks!





Re: Windows Presentation Foundation (WPF) How to: data template int[]?

Marlon Grech

hi there have a lokk at this....

http://www.beacosta.com/2006/09/how-can-i-control-color-using-sliders.html

maybe you can use the ConvertBack method of the converter to set the values in the Collection you have as source. (Please not that the example in the link does not use two way binding but you can do it by using the ConvertBack method of the converter)...

If you need any more help please let me know and I will try to do some sample code for you...





Re: Windows Presentation Foundation (WPF) How to: data template int[]?

Sam Bent - MSFT

If possible, you should replace the array of ints with an array of Band objects, where Band has a property called Value of type int. Then you can use the markup from your first post. [You don't need to say TwoWay - that's the default for Slider.Value.]

When you use an ItemsControl to display an array, each Slider is bound to the value in the corresponding array position - not to the array position itself. If the value is an int, the slider doesn't know where to write the new value. But when the value is a Band, the slider knows to write the new value into the Band's Value property. [The same problem applies to ObservableCollection<int>. You don't need ObservableCollection here, since you're not adding or removing bands.]






Re: Windows Presentation Foundation (WPF) How to: data template int[]?

Perter

I would like to try this but may not be able to change to Band objects since the framework is already used by other apps and will take some work to change everything.

Let me try a few things and I'll update a status. Thanks to both for the input - it all helped.