หลังจากอ่าน บทความของ Shawn แนะนำโดย @Depechie ฉันพยายามใช้โซลูชันเดียวกันนี้ในแอปของฉันเพื่ออัปเดตเนื้อหาของ AppBar ด้วยปุ่มเฉพาะส่วน
แม้ว่าฉันจะพยายามแล้ว แต่ฉันก็ไม่สามารถทำงานได้ ดังนั้นฉันจึงปรับเปลี่ยนวิธีแก้ปัญหาบางส่วน ฉันใช้วิธีแก้ปัญหาพฤติกรรมและเปลี่ยนเฉพาะ ScrollerOnViewChanged
function ดังนี้ นี่อาจไม่ใช่วิธีที่ดีที่สุดหรืออาจทำให้เกิดผลลัพธ์ที่ไม่คาดคิดในสถานการณ์ต่างๆ แต่ในกรณีของฉันมันใช้งานได้โดยไม่มีปัญหา
private void ScrollerOnViewChanged(object sender, ScrollViewerViewChangedEventArgs scrollViewerViewChangedEventArgs)
{
_settingIndex = true;
ScrollViewer scrollViewer = sender as ScrollViewer;
if (scrollViewer.HorizontalOffset > (scrollViewer.ViewportWidth / 2))
SelectedIndex = 1;
else
SelectedIndex = 0;
_settingIndex = false;
}
หลังจากนั้นฉันได้เพิ่มคุณสมบัติใน viewmodel ของฉันเพื่อจัดเก็บดัชนีที่เลือก
private int _selectedIndex;
public int SelectedIndex
{
get { return _selectedIndex; }
set
{
SetProperty(ref this._selectedIndex, value);
}
}
ฉันใช้ลักษณะการทำงานใน XAML เพื่ออัปเดต SelectedIndex ใน ViewModel ของฉัน
<Hub>
<i:Interaction.Behaviors>
<behaviors:HubSelectionBehavior SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" />
</i:Interaction.Behaviors>
<HubSection>...</HubSection>
<HubSection>...</HubSection>
</Hub>
สิ่งสุดท้ายที่ต้องทำคือตั้งค่าการเปิดเผย AppBarButtons โดยใช้คุณสมบัตินี้ SectionIndexToVisibilityConverter เปรียบเทียบ SelectedIndex กับ ConverterParameter และส่งกลับ Visibility.Visible
หากเท่ากัน
<CommandBar>
<AppBarButton Label="Open" Icon="World" Command="{Binding OpenInBrowserCommand}" Visibility="{Binding SelectedIndex, Converter={StaticResource SectionIndexToVisibilityConverter}, ConverterParameter=0}"/>
<AppBarButton Label="Previous" Icon="Back" Command="{Binding PreviousAnswerCommand}" Visibility="{Binding SelectedIndex, Converter={StaticResource SectionIndexToVisibilityConverter}, ConverterParameter=1}"/>
<AppBarButton Label="Next" Icon="Forward" Command="{Binding NextAnswerCommand}" Visibility="{Binding SelectedIndex, Converter={StaticResource SectionIndexToVisibilityConverter}, ConverterParameter=1}"/>
</CommandBar>
ขอบคุณ @Depechie ที่แนะนำบทความและ @Shawn ที่เขียนบทความ :)
person
Birkan Cilingir
schedule
05.02.2015
HorizontalOffset
ของScrollViewer
เพื่อตัดสินใจว่าจะอยู่ในส่วนแรกหรือส่วนที่สอง ขอบคุณสำหรับลิงค์ - person Birkan Cilingir   schedule 23.01.2015