将控制值与具有值列表的属性结合。有可能吗?
我有一些窗口,其中有一些控件(时间段选择器):
更新
<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" />
在窗口类中我做了属性:
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 }
};
现在我想做一些事情,当我更改 Groupbox 中的值时更新滑块值。有可能做到吗?
我已经这样做了:
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;
}
但对我来说这不是一个很好的解决方案。 也许您知道更简单的方法?
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 solution.
Maybe you know simplier way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我删除了您的设计属性,以使解决方案看起来更清洁。
在您的代码中保留您的
timeperiodtype
s expect。适当设置以允许您绑定到所使用的背景类的成员。
然后,将
ListBox
(或其他选择器
Control)绑定到该列表:现在,您可以将
slider
的属性直接绑定到属性selectedItem
的listbox
(这实际上是timeperiodtype
,因为先前的绑定):最后,您可以将标签绑定到分配给
slider
的值:I removed your design attributes to make the solution look a little cleaner.
Keep your list of
TimePeriodType
s exposed in your code: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 otherSelector
control) to that list:Now, you can directly bind the properties of the
Slider
to the properties of theSelectedItem
of theListBox
(which is really aTimePeriodType
because the of the prior binding):Finally, you can bind the labels to the values assigned to the
Slider
:好吧,我找到了解决方案。
应该有带有 get 方法的属性:
另外还应该有以下绑定:
Ok I found solution.
There should be property with get method:
And in addition there should be following binding: