gpt4 book ai didi

wpf - MVVM 中的绑定(bind) slider 值

转载 作者:行者123 更新时间:2023-12-03 10:14:13 25 4
gpt4 key购买 nike

我在 MVVM 中的 slider 值数据绑定(bind)中遇到问题。当值(value)发生变化时,我的预期值(value)没有实现。我该如何解决我的问题?
我有一个列表框、一个 slider 和一个文本 block 。列表框绑定(bind)到 ListImage, slider 值和文本 block 文本绑定(bind)到 CurrentImage。使用命令的一个按钮导航 lisbox 项目。 CurrentImage 是 View 模型中的一个属性。当我更改 slider 的 setter 时, setter 的新值将变为 slider setter 的当前值,并且列表框的排列被破坏。例如,当我的 slider setter 的值设置为 50 时,我再次将 slider 的值更改为 10。我的 slider 值从 10 导航到 50,而不是更多。它必须导航整个列表框,但它不能。有我的代码:

XAML:

<TextBlock Text="{Binding CurrentImage.Index}"/>
<Slider Height="23" HorizontalAlignment="Left" Margin="12,305,0,0" Name="slider1" VerticalAlignment="Top" Width="479" Maximum="{Binding ListImage.Count, Mode=OneTime}"
Value="{Binding CurrentImage.Index, Mode=TwoWay}"
SmallChange="1" />
<Button Content="{Binding DisplayPlay}" Command="{Binding PlayCommand}" Height="23" HorizontalAlignment="Left" Margin="507,305,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<ListBox Height="129" HorizontalAlignment="Left" Margin="12,334,0,0" ItemsSource="{Binding ListImage}" SelectedItem="{Binding CurrentImage,Mode=TwoWay}"
VerticalAlignment="Top" Width="472">

View 模型:

public class MainViewModel : ViewModelBase
{
public ICommand PlayCommand { get; set; }
private DispatcherTimer _Timer;
public ImageDTO Image { get; set;}
DataAccess AC = new DataAccess();
public byte[] Bytes { get; set; }
public MainViewModel()
{
ListImage = AC.OpenImages();
CurrentImage = ListImage[0];
Bytes = CurrentImage.Bytes;
this.PlayCommand = new DelegateCommand(Play, CanPlay);
DisplayPlay = "Play";
_Timer = new DispatcherTimer();
_Timer.Interval = new TimeSpan(0, 0, 0, 0, 2000 / 30);
_Timer.Tick += new EventHandler(timer_Tick);
}

private string _DisplayPlay;
public string DisplayPlay
{
get { return _DisplayPlay; }
set
{
if (_DisplayPlay != value)
{
_DisplayPlay = value;
OnPropertyChanged("DisplayPlay");
}
}
}

private List<ImageDTO> _ListImage;
public List<ImageDTO> ListImage
{
get { return _ListImage; }
set
{
if (_ListImage != value)
_ListImage = value;
OnPropertyChanged("ListImage");
}
}

private ImageDTO _CurrentImage;
public ImageDTO CurrentImage
{
get { return _CurrentImage; }
set
{
if (_CurrentImage != value)
{
_CurrentImage = value;
OnPropertyChanged("CurrentImage");
}
}
}

public bool CanPlay(object parameter)
{
return true;
}
public void Play(object parameter)
{
if (DisplayPlay == "Play")
{
DisplayPlay = "Pause";
_Timer.Start();
}
else
{
_Timer.Stop();
DisplayPlay = "Play";
}
}

private void timer_Tick(object sender, EventArgs e)
{
int position = ListImage.FindIndex(x => x.Index == CurrentImage.Index);
position++;
if (position == ListImage.Count)
{
position = 0;
}
else
{
CurrentImage = ListImage[position];
}
}

最佳答案

也许这就是你想要的:

<StackPanel>
<ListBox x:Name="ImageListBox" IsSynchronizedWithCurrentItem="True">
<ListBoxItem>Image1</ListBoxItem>
<ListBoxItem>Image2</ListBoxItem>
<ListBoxItem>Image3</ListBoxItem>
<ListBoxItem>Image4</ListBoxItem>
</ListBox>
<Slider Value="{Binding ElementName=ImageListBox, Path=SelectedIndex}"
Maximum="{Binding ElementName=ImageListBox, Path=Items.Count}"/>
</StackPanel>

您可能希望比此示例更好地处理最大值

关于wpf - MVVM 中的绑定(bind) slider 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14339515/

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