gpt4 book ai didi

c# - 将时间跨度绑定(bind)到 slider

转载 作者:太空宇宙 更新时间:2023-11-03 20:08:36 24 4
gpt4 key购买 nike

我的程序中有一个间隔保存为 TimeSpan。我想将该值绑定(bind)到 Slider 以轻松调整 TimeSpan。我的计划是这样的,但它不起作用(它什么都不做):

编辑:根据答案更改了一些代码,但我仍然遇到同样的问题

public partial class MainWindow : Window, INotifyPropertyChanged
{

...

TimeSpan measure_gap = TimeSpan.FromSeconds(1); //init
public double Measure_gap
{
get
{
return measure_gap.TotalSeconds;
}
set
{
if (value != measure_gap.TotalSeconds)
{
measure_gap = TimeSpan.FromSeconds(value);
OnPropertyChanged("Measure_gap");
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

这是我的绑定(bind)(我认为问题出在这里)

<Slider HorizontalAlignment="Left" Height="25" Margin="38,202,0,0" 
VerticalAlignment="Top" Width="217" Minimum="1" Maximum="10" SmallChange="1"
Value="{Binding ElementName=MainWindow, Path=Measure_gap, Mode=TwoWay}"/>

所以我的问题是,我做错了什么 :-) 谢谢编辑:this..BindingContext = this

最佳答案

是的,问题是更改的值不会更新到绑定(bind)。在这里我详细说明一下,

public partial class MainWindow : Window, INotifyPropertyChanged
{

TimeSpan measure_gap = TimeSpan.FromSeconds(1); //init
public double Measure_gap
{
get
{
return measure_gap.TotalSeconds;
}
set
{
if (value != measure_gap.TotalSeconds)
{
measure_gap = TimeSpan.FromSeconds(value);
OnPropertyChanged("Measure_gap");
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

关于c# - 将时间跨度绑定(bind)到 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com