I have windows where I have some controls(time period picker):
UPDATED
<ComboBox DisplayMemberPath="{Binding Path=Name}" ItemsSource="{Binding Periods}" Name="timeType" />
<Slider Value="20" Minimum="{Binding SelectedItem.Min, ElementName=timeType}" Maximum="{Binding SelectedItem.Max, ElementName=timeType}" Name="timeSlider" />
<Label Content="{Binding ElementName=timeSlider, Path=Value}" Name="timeValue" />
<Label Content="{Binding ElementName=timeSlider, Path=Minimum}" Name="timeValueMin" />
<Label Content="{Binding ElementName=timeSlider, Path=Maximum}" Name="timeValueMax" />
In window class I did property:
public class TimePeriodType {
public string Name { set; get; }
public int Min { set; get; }
public int Max { set; get; }
}
public List<TimePeriodType> Periods = new List<TimePeriodType>() {
new TimePeriodType() { Name="Hours", Max=6, Min=1 },
new TimePeriodType(){ Name="Minutes", Max=59, Min=20 }
};
And now I want to do somethink to update Slider values when I change value in Groupbox. Is there any possibility to do it?
Already I do it like this:
private void timeType_SelectionChanged( object sender, SelectionChangedEventArgs e ) {
var period = Periods.Single(p => p.Name == timeType.SelectedValue.ToString());
timeSlider.Minimum = period.Min;
timeSlider.Maximum = period.Max;
}
But for me it is not great solut开发者_Python百科ion. Maybe you know simplier way to do it?
I removed your design attributes to make the solution look a little cleaner.
Keep your list of TimePeriodType
s exposed in your code:
public List<TimePeriodType> Periods = new List<TimePeriodType>() {
new TimePeriodType() { Name="Hours", Max=6, Min=1 },
new TimePeriodType() { Name="Minutes", Max=59, Min=20 }
};
Make sure your DataContext
is appropriately set to allow you to bind to members of the backing class you're using.
Then, bind the ListBox
(or other Selector
control) to that list:
<ListBox ItemsSource="{Binding Periods}"
Name="timeType" />
Now, you can directly bind the properties of the Slider
to the properties of the SelectedItem
of the ListBox
(which is really a TimePeriodType
because the of the prior binding):
<Slider Maximum="{Binding SelectedItem.Max, ElementName=timeType}"
Minimum="{Binding SelectedItem.Min, ElementName=timeType}"
Name="timeSlider" />
Finally, you can bind the labels to the values assigned to the Slider
:
<Label Content="{Binding Value, ElementName=timeSlider}"
Name="timeValue" />
<Label Content="{Binding Minimum, ElementName=timeSlider}"
Name="timeValueMin" />
<Label Content="{Binding Maximum, ElementName=timeSlider}"
Name="timeValueMax" />
Ok I found solution. There should be property with get method:
public class TimePeriodType {
public string Name { set; get; }
public int Min { set; get; }
public int Max { set; get; }
}
List<TimePeriodType> _periods = new List<TimePeriodType>() {
new TimePeriodType() { Name="Hours", Max=6, Min=1 },
new TimePeriodType() { Name="Minutes", Max=59, Min=20 }
};
public List<TimePeriodType> Periods {
get { return _periods; }
set { _periods = value; }
}
And in addition there should be following binding:
ItemsSource="{Binding Periods, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
精彩评论