MVVM のスライダー値のデータ バインディングに問題があります。値が変更されると、期待値が達成されません。どうすれば問題を解決できますか?
リストボックス、スライダー、テキストブロックがあります。listbox は ListImage にバインドされ、スライダーの値と textblock テキストは CurrentImage にバインドされます。コマンド付きの 1 つのボタンで、リスボックス アイテムをナビゲートします。CurrentImage はビューモデルのプロパティです。スライダーのセッターを変更すると、セッターの新しい値がスライダーのセッターの現在の値に置かれ、リストボックスの配置が崩れます。たとえば、スライダーのセッターの値を 50 に設定し、スライダーの値を再び 10 に変更するとします。私のスライダーの値は 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">
ビューモデル:
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];
}
}