2015-01-22 5 views
1

FlipView의 SelectedIndex를 프로그래밍 방식으로 변경하고 싶습니다. 내 뷰 모델은 다음과 같습니다WinRT FlipView.SelectedIndex 바인딩이 ViewModel의 변경 내용에 응답하지 않습니다.

public class MyViewModel : ViewModelBase { 
    private int _flipViewIndex; 

    public int FlipViewIndex 
    { 
     get { return _flipViewIndex; } 
     private set { Set(ref _flipViewIndex, value); } 
    } 

    private string _logText; 

    public string LogText 
    { 
     get { return _logText; } 
     private set { Set(ref _logText, value); } 
    } 

    public async void Log(string text) 
    { 
     CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher; 
     if (dispatcher.HasThreadAccess) 
     { 
     LogText += text + "\n"; 
     } 
     else 
     { 
     await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => Log(text)); 
     } 
    } 

    public async void SetIndex(int index) 
    { 
     CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher; 
     if (dispatcher.HasThreadAccess) 
     { 
     FlipViewIndex = index; 
     } 
     else 
     { 
     await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => SetIndex(index)); 
     } 
    } 
} 

Set()INotifyPropertyChanged.PropertyChanged()를 발생시킵니다.

<views:BaseView> 
<Grid DataContext="{StaticResource ViewModel}"> 
    <FlipView SelectedIndex="{Binding FlipViewIndex}"> 
    <Control1 /> 
    <Control2 /> 
    <Control3 /> 
    </FlipView> 
    <TextBlock Text="{Binding LogText}" /> 
    </Grid> 
</views.BaseView> 

뷰와 뷰 모델이 제대로 준수 할 나타납니다

내 XAML은 다음과 같습니다. 내 컨트롤러에서 ViewModel.Log("foo")으로 전화하면 TextBlock의 텍스트가 변경되어 변경 내용을 반영합니다.

ViewModel.SetIndex(n)으로 전화를 걸 때 FlipView의 SelectedIndexn으로 업데이트되지 않으며 단지 0에 머물러 있습니다. 그 이유는 무엇입니까?

+0

확실하지 않습니다. SelectedItem을 옵션으로 사용하고 있습니까? –

+0

안녕하세요, Jerry - 시도하지는 않았지만 다른 앱에서 ViewModel을 더 명확하게 분리 할 수 ​​있도록 SelectedItem을 사용하지 않는 것이 좋습니다. –

+0

당신은 내가하는 것보다 당신의 앱을 더 잘 압니다. 그러나 우리가 그것에 대해 이야기하고 있기 때문에, 선택된 항목에 대한 참조가 정렬 된 목록에서의 순서 위치보다보기와 관련이 적다는 것을 지적하는 것은 가치가 있습니다. 내 사용자 중 선택된 항목이 Jerry라고합니다. 내 사용자 중 내가 선택한 색인은 ... 뭐라고 말합니까? 가시성을 고려합니까? 정렬을 고려합니까? 필터 계정은 있습니까? SelectedItem은 간단합니다. SelectedIndex는 뷰 모델이보기와 친밀 해 지도록 관심사를 혼합합니다. 틀렸어? MVVM은 융통성이 있으며 잘못이 없습니다. 그러나 인덱스는 더 이상 분리되어 있습니까? 제 생각에는 아닙니다. –

답변

3

나는 이것이 작동하는 것을 확인했습니다.

<FlipView FontSize="200" 
      ItemsSource="{Binding Items}" 
      SelectedIndex="{Binding Index, Mode=TwoWay}" /> 

이렇게하면됩니다.

<Page.BottomAppBar> 
    <CommandBar> 
     <AppBarButton Command="{Binding PrevCommand}" 
         Icon="Previous" 
         Label="Previous" /> 
     <AppBarButton Command="{Binding NextCommand}" 
         Icon="Next" 
         Label="Next" /> 
    </CommandBar> 
</Page.BottomAppBar> 

그리고이보기 모델

public class MyViewModel : BindableBase 
{ 
    public MyViewModel() 
    { 
     foreach (var item in new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }) 
      this.Items.Add(item); 
    } 

    ObservableCollection<int> _Items = new ObservableCollection<int>(); 
    public ObservableCollection<int> Items { get { return _Items; } } 

    int _Index = default(int); 
    public int Index { get { return _Index; } set { base.SetProperty(ref _Index, value); } } 

    public DelegateCommand PrevCommand 
    { 
     get { return new DelegateCommand(() => { this.Index--; }); } 
    } 

    public DelegateCommand NextCommand 
    { 
     get { return new DelegateCommand(() => { this.Index++; }); } 
    } 
} 

작품. 정말 않습니다.

행운을 빈다.

+0

나는 여기에서 시작하여 단절된 부분을보기 위해 단계적으로 조정할 것이다. 당신의 도움을 주셔서 감사합니다! –

+0

위대한 작품! 나는 XAML에서'Mode = TwoWay'를 놓쳤다. –