2017-09-04 43 views
1

我試圖綁定一個可重用的按鈕在一個carusel,我想要實現的是一個添加讓我們說6個按鈕每個按鈕將有一個命令,按照按鈕名稱將導航到正確的頁面。如何創建可重用的UserControl併爲其綁定命令?

我能做到這一點通過這樣做:

<toolkitcontrols:Carousel x:Name="NavigationMenuCarouselPanel" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             Orientation="Horizontal" 
             ItemsSource="{x:Bind ViewModel.MenuList, Mode=OneWay}" 
             ItemMargin="25" 
             ItemDepth="160" 
             ItemRotationX="180" 
             ItemRotationY="25" 
             ItemRotationZ="0" 
             SelectedIndex="2" 
             Grid.Row="1"> 
       <toolkitcontrols:Carousel.EasingFunction> 
        <CubicEase EasingMode="EaseOut"/> 
       </toolkitcontrols:Carousel.EasingFunction> 
        <Button Command="{x:Bind ViewModel.NavigateToPage1, Mode=OneWay}" 
          Content="{x:Bind ViewModel.Name, Mode=OneWay}"/> 
      </toolkitcontrols:Carousel> 

如果我這樣做,我會添加5個按鈕和我得寫爲每個按鈕的特性。

所以不是我想用一個用戶控件,只是寫的是這樣的:

<toolkitcontrols:Carousel x:Name="NavigationMenuCarouselPanel" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             Orientation="Horizontal" 
             ItemsSource="{x:Bind ViewModel.MenuList, Mode=OneWay}" 
             ItemMargin="25" 
             ItemDepth="160" 
             ItemRotationX="180" 
             ItemRotationY="25" 
             ItemRotationZ="0" 
             SelectedIndex="2" 
             Grid.Row="1"> 
       <toolkitcontrols:Carousel.EasingFunction> 
        <CubicEase EasingMode="EaseOut"/> 
       </toolkitcontrols:Carousel.EasingFunction> 
       <toolkitcontrols:Carousel.ItemTemplate> 
        <DataTemplate x:DataType="data:ButtonInfo"> 
         <usercontrolvm:NavigationMenuButtonTemplate NavigateToPageCommand="{Binding NavigateToPageCommand}"/> 
        </DataTemplate> 
       </toolkitcontrols:Carousel.ItemTemplate> 
      </toolkitcontrols:Carousel> 

但我已經失敗了這樣做,我發現了一些教程,但都按照我的理解將讓我寫這個喜歡的代碼:

<usercontrolvm:NavigationMenuButtonTemplate NavigateToPageCommand="{Binding NavigateToPageCommand}"/> 

喜歡6次,我不知道它將如何將DataType的DataTemplate爲我的屬性列表。

這是我UserControl.xaml.cs

public sealed partial class NavigationMenuButtonTemplate : UserControl 
{ 
    public ButtonInfo ButtonInfo => (DataContext as ButtonInfo); 

    public NavigationMenuButtonTemplate() 
    { 
     this.InitializeComponent(); 
     Loaded += NavigationMenuButtonTemplate_Loaded; 
    } 

    private void NavigationMenuButtonTemplate_Loaded(object sender, RoutedEventArgs e) 
    { 
     Bindings.Update(); 
    } 

    public DelegateCommand NavigateToPageCommand 
    { 
     get { return (DelegateCommand)GetValue(NavigateToPageCommandProperty); } 
     set { SetValue(NavigateToPageCommandProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for NavigateToPageCommand. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty NavigateToPageCommandProperty = 
     DependencyProperty.Register("NavigateToPageCommand", 
      typeof(DelegateCommand), 
      typeof(NavigationMenuButtonTemplate), 
      new PropertyMetadata(0)); 
} 

這是我ButtonInfo.cs

public class ButtonInfo 
{ 
    public string Symbol { get; set; } 
    public string FontFamily { get; set; } 
    public string MenuName { get; set; } 
    public string BenefitKind { get; set; } 
    public string Status { get; set; }   
} 

,這是我UserControl.xaml

<Button x:Name="NavigationMenuTemplate" 
      Width="300" 
      Height="300" 
      Command="{Binding NavigateToPageCommand, ElementName=root, Mode=OneWay}"> 
     <Grid x:Name="ButtonLayout"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock x:Name="NavigationMenuIconTextBlock" 
         Grid.Row="0" 
         Grid.Column="0" 
         Grid.ColumnSpan="2" 
         FontFamily="{x:Bind ButtonInfo.FontFamily, Mode=OneWay, FallbackValue='Webdings'}" 
         Text="{x:Bind ButtonInfo.Symbol, Mode=OneWay, FallbackValue='&#x91;'}" 
         FontSize="150" 
         Foreground="Black" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Center"/> 
      <TextBlock x:Name="NavigationMenuButtonNameTextBlock" 
         Grid.Row="1" 
         Grid.Column="0" 
         Grid.ColumnSpan="2" 
         Text="{x:Bind ButtonInfo.MenuName, Mode=OneWay, FallbackValue='CALCULADORA JORNADAS EXTRAORDINARIAS'}" 
         FontSize="12" 
         Foreground="Black" 
         HorizontalAlignment="Center"/> 
      <TextBlock x:Name="NavigationMenuButtonBenefitKindTextBlock" 
         Grid.Row="2" 
         Grid.Column="0" 
         Text="{x:Bind ButtonInfo.BenefitKind, Mode=OneWay, FallbackValue='Subscripción'}" 
         FontSize="10" 
         Foreground="Black" 
         HorizontalAlignment="Left"/> 
      <TextBlock x:Name="NavigationMenuButtonStatusTextBlock" 
         Grid.Row="2" 
         Grid.Column="1" 
         Text="{x:Bind ButtonInfo.Status, Mode=OneWay, FallbackValue='Vigente'}" 
         FontSize="10" 
         Foreground="Black" 
         HorizontalAlignment="Right"/> 
     </Grid>    
    </Button> 

能有人幫助我請指點我正確的方向。 我錯過了什麼?

+0

所有我能理解你不想定義按鈕或usercontrols 6次。如果用戶控件是一種可以基於綁定輸入填充UI的項目控件,那將是可能的。在你的情況下,用戶控件只保存1個按鈕,這將不起作用。 – Dilmah

+1

當您使用UWP時,您可以在Click中使用'x:bind'並且您可以清除命令。 – lindexi

+0

這可能是你正在尋找的https://github.com/StuartSmith/UWP-Template10-Carousel-Sample。 –

回答

3

您的問題中的ItemTemplate方法實際上是在正確的軌道上。

最後,你的XAML將類似於以下(只有少數屬性包括在內,但你的想法)的東西 -

<toolkitcontrols:Carousel ItemsSource="{x:Bind ButtonInfoCollection}"> 
    <toolkitcontrols:Carousel.ItemTemplate> 
     <DataTemplate x:DataType="local:ButtonInfo"> 
      <local:NavigationMenuButton NavigateToPageCommand="{Binding DataContext.NavigateToPageCommand, ElementName=MyPageName}" 
             NavigateToPageCommandParameter="{x:Bind PageType}" 
             MenuName="{x:Bind MenuName}" 
             SymbolPath="{x:Bind Symbol}" /> 
     </DataTemplate> 
    </toolkitcontrols:Carousel.ItemTemplate> 
</toolkitcontrols:Carousel> 

隨着結構之上記,你只要需要在NavigationMenuButton用戶控件中將這些屬性公開爲依賴項屬性。請參閱下面一個簡單的例子 -

NavigationMenuButton XAML

<UserControl x:Class="DesignTest.NavigationMenuButton"> 

    <!--If any of the properties can be updated, change the binding Mode to OneWay--> 
    <Button Command="{x:Bind NavigateToPageCommand, Mode=OneWay}" CommandParameter="{x:Bind NavigateToPageCommandParameter}"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="20" /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 

      <Image x:Name="SymbolImage" Stretch="UniformToFill" /> 
      <TextBlock Text="{x:Bind MenuName, FallbackValue='JORNADAS EXTRAORDINARIAS', TargetNullValue='JORNADAS EXTRAORDINARIAS'}" Grid.Column="1" /> 
     </Grid> 
    </Button> 
</UserControl> 

NavigationMenuButton代碼隱藏

public sealed partial class NavigationMenuButton : UserControl 
{ 
    public NavigationMenuButton() 
    { 
     InitializeComponent(); 
    } 

    public ICommand NavigateToPageCommand 
    { 
     get => (ICommand)GetValue(NavigateToPageCommandProperty); 
     set => SetValue(NavigateToPageCommandProperty, value); 
    } 
    public static readonly DependencyProperty NavigateToPageCommandProperty = DependencyProperty.Register(
     "NavigateToPageCommand", typeof(ICommand), typeof(NavigationMenuButton), new PropertyMetadata(null)); 

    public object NavigateToPageCommandParameter 
    { 
     get => GetValue(NavigateToPageCommandParameterProperty); 
     set => SetValue(NavigateToPageCommandParameterProperty, value); 
    } 
    public static readonly DependencyProperty NavigateToPageCommandParameterProperty = DependencyProperty.Register(
     "NavigateToPageCommandParameter", typeof(object), typeof(NavigationMenuButton), new PropertyMetadata(null)); 

    public string MenuName 
    { 
     get => (string)GetValue(MenuNameProperty); 
     set => SetValue(MenuNameProperty, value); 
    } 
    public static readonly DependencyProperty MenuNameProperty = DependencyProperty.Register(
     "MenuName", typeof(string), typeof(NavigationMenuButton), new PropertyMetadata(null)); 

    public string SymbolPath 
    { 
     get => (string)GetValue(SymbolPathProperty); 
     set => SetValue(SymbolPathProperty, value); 
    } 
    public static readonly DependencyProperty SymbolPathProperty = DependencyProperty.Register(
     "SymbolPath", typeof(string), typeof(NavigationMenuButton), new PropertyMetadata(null, (s, e) => 
     { 
      // We don't do the x:Bind for this property in XAML because the Image control's Source property 
      // doesn't accept a string but a BitmapImage, so one workaround is to do the conversion here. 

      var self = (NavigationMenuButton)s; 
      var image = self.SymbolImage; 
      var symbolPath = (string)e.NewValue; 

      image.Source = new BitmapImage(new Uri(self.BaseUri, symbolPath ?? "/Assets/default_path")); 
     })); 
} 

注意你需要在你的ButtonInfo類導航PageType屬性目的。

public Type PageType { get; set; } 

我個人不喜歡具有在項目級別(即,在ButtonInfo類)中定義的導航命令,相反,我使用ElementNameCarousel的數據模板結合來搜索一個水平和綁定到頁面的DataContext(頁面的ViewModel)中定義的NavigateToPageCommand

這意味着該ViewModel將同時擁有ButtonInfoCollectionNavigateToPageCommand像下面定義 -

public ObservableCollection<ButtonInfo> ButtonInfoCollection { get; } = new ObservableCollection<ButtonInfo> 
{ 
    new ButtonInfo { MenuName = "New Menu", PageType = typeof(BlankPage1), Symbol = "/Assets/StoreLogo.png" } 
}; 

public DelegateCommand<Type> NavigateToPageCommand { get; } = new DelegateCommand<Type>(type => 
    App.Frame.Navigate(type)); 

我希望這一切纔有意義。祝你好運!

+0

美麗的答案,我想我也喜歡我的答案,但在我看來你的建築非常漂亮。感謝分享! –

0

好的,Firstable to Dilmah總是可以在任何itemTemplate中構建一個可重用的用戶控件。我會告訴你現在和在這裏。

我已經想出了兩個解決方案,第一個解決方案,它激發了我在閱讀大量關於databing {x:Bind}和{Binding}標記後尋找的內容,我可以學習如何創建一個可重用的UserControlTemplate

1號液

首先,我會告訴你如何創建一個導航菜單,包括carusel控制,它可以在NuGet包Microsoft.Toolkit.Uwp.UI.Control找到

所以這是我的代碼現在在我的MainMenuPage參考carusel控制:

<toolkitcontrols:Carousel x:Name="NavigationMenuCarouselPanel" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             Orientation="Horizontal" 
             ItemsSource="{x:Bind ViewModel.NavMenuButtonVMs}" 
             ItemMargin="25" 
             ItemDepth="160" 
             ItemRotationX="180" 
             ItemRotationY="25" 
             ItemRotationZ="0" 
             SelectedIndex="0" 
             Grid.Row="1"> 
       <toolkitcontrols:Carousel.EasingFunction> 
        <CubicEase EasingMode="EaseOut"/> 
       </toolkitcontrols:Carousel.EasingFunction> 
       <toolkitcontrols:Carousel.ItemTemplate> 
        <DataTemplate> 
         <usercontrolvm:NavigationMenuButtonTemplate/> 
        </DataTemplate> 
       </toolkitcontrols:Carousel.ItemTemplate>     
      </toolkitcontrols:Carousel> 

這段代碼的這一重要組成部分,是的ItemSource屬性,它是X:綁定到我的NavMenuButtonVms的ObservableCollection,和我的用戶被包裹withing Carousel.ItemTemplate和DataTemplate的標籤這將使得我們能夠爲我們的重用代碼並在我們的列表中創建N個控件。

下一個是我爲我的MainMenuPage視圖模型:

public class MainMenuPageViewModel : Mvvm.ViewModelBase 
{ 
    ObservableCollection<NavigationMenuButtonTemplateViewModel> _NavMenuButtonVMs = default(ObservableCollection<NavigationMenuButtonTemplateViewModel>); 

    public MainMenuPageViewModel() 
    { 
     Shell.HamburgerMenu.IsFullScreen = false; 
     NavMenuButtonVMs = GetNavMenuButtonInfo(); 
    } 

    public string Title => GetLocalizeString("MainMenuPageViewModelTitle"); 
    public ObservableCollection<NavigationMenuButtonTemplateViewModel> NavMenuButtonVMs 
    { 
     get { return _NavMenuButtonVMs; } 
     private set { Set(ref _NavMenuButtonVMs, value); } 
    } 

    public override Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state) 
    { 
     NavigationService.ClearHistory(); 
     return base.OnNavigatedToAsync(parameter, mode, state); 
    }  
} 

正如你可以看到我初始化我的構造函數中我的ObservableCollection。 GetNavMenuButton()方法是Helpers命名空間中的一個靜態類,但我會告訴你代碼,以便你可以對如何種子列表有所瞭解,也可以注意到我沒有調用靜態類,因爲我使用C#6.0語法,你可以直接在你的類中調用靜態方法。

,你可以添加一個using語句靜態類像這樣的:

using static Ceneam.Helpers.NavigationMenuButtonViewModelHelper; 

這個聲明,您可以使用這樣一個靜態方法:

GetNavMenuButtonInfo(); 

,而不是這樣的:

NavigationMenuButtonViewModelHelper.GetNavMenuButtonInfo(); 

我解釋過這種情況,如果你不明白我的代碼。

然後我將創建我的用戶控件,我將向您展示xaml,xaml.cs以及viewmodel。 注意usercontrol中的綁定標記,因爲您必須使用x:綁定在可重用的用戶控件中進行綁定。

這是我NavigationMenuButtonTemplate.xaml

<UserControl 
x:Class="Ceneam.UserControlViews.NavigationMenuButtonTemplate" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Ceneam.UserControlViews" 
xmlns:vm="using:Ceneam.ViewModels" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="400" 
d:DesignWidth="400"> 

<Grid VerticalAlignment="Center" 
     HorizontalAlignment="Center">   
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 

    <Button x:Name="NavigationMenuTemplate" 
      Command="{Binding NavigateToPageCommand, Mode=OneWay}"> 
     <Grid x:Name="ButtonLayout"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 

      <Image x:Name="NavigationMenuIconImage" 
        Source="{Binding ButtonInfo.Symbol, Mode=OneWay, FallbackValue='ms-appx:///Assets/AssetsMainMenuPage/OverTimeMoneyWhite256x256.png'}" 
        PointerEntered="NavigationMenuIconImage_PointerEntered"/> 
      <TextBlock x:Name="NavigationMenuButtonNameTextBlock" 
         Text="{Binding ButtonInfo.MenuName, Mode=OneWay, FallbackValue='JORNADAS EXTRAORDINARIAS'}"/> 
      <TextBlock x:Name="NavigationMenuButtonBenefitKindTextBlock" 
         Text="{Binding ButtonInfo.BenefitKind, Mode=OneWay, FallbackValue='Subscripción'}"/> 
      <TextBlock x:Name="NavigationMenuButtonStatusTextBlock" 
         Text="{Binding ButtonInfo.Status, Mode=OneWay, FallbackValue='Vigente'}"/> 
     </Grid>    
    </Button> 
</Grid> 

,你可以看到我只使用綁定標記和的原因是因爲我用的視圖模型與參數的權利,我不得不創建在我的用戶的依賴:

public class NavigationMenuButtonTemplateViewModel : Mvvm.ViewModelBase 
{ 
    ButtonInfo _ButtonInfo = default(ButtonInfo); 

    public NavigationMenuButtonTemplateViewModel() { } 
    public NavigationMenuButtonTemplateViewModel(ButtonInfo buttonInfo) 
    { 
     ButtonInfo = new ButtonInfo 
     { 
      BenefitKind = buttonInfo.BenefitKind, 
      Status = buttonInfo.Status, 
      MenuName = buttonInfo.MenuName, 
      Symbol = buttonInfo.Symbol 
     }; 
    } 

    public ButtonInfo ButtonInfo 
    { 
     get { return _ButtonInfo; } 
     set { Set(ref _ButtonInfo, value); } 
    } 

    public DelegateCommand NavigateToPageCommand => new DelegateCommand(async() => { await ExecuteNavigateToPageCommand(); }); 

    private async Task ExecuteNavigateToPageCommand() 
    { 
     var message = new MessageDialog("Test"); 
     await message.ShowAsync(); 
    } 
} 

,因爲你創建的視圖模型參數的構造器我不是能夠創造一個強類型綁定與構造uctor這是讓我留下後面的主要原因:綁定標記爲我的用戶控件,這意味着你不能在事件上使用x:bind方法。您將不得不在usercontrol的xaml.cs文件中使用樣式化方法。

如果您在聲明XAML是這樣的:

<UserControl.DataContext> 
    <vm:usercontrol x:Name=ViewModel/> 
<UserControl.DataContext> 

它將始終觸發參數的構造函數擺脫你的初始化值的更差越來越NullReferenceException異常,你也可以使用的DataContext的設計時的數據,但它必須在文件的開頭聲明,我不會在這裏集中。

,並最終在我的靜態類是我在他們這個參數創建我的UC(用戶控件)是我的靜態類:

public static class NavigationMenuButtonViewModelHelper 
{ 
    public static ObservableCollection<NavigationMenuButtonTemplateViewModel> GetNavMenuButtonInfo() 
    { 
     var data = new ObservableCollection<NavigationMenuButtonTemplateViewModel>(); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/SatSunBonusWhite256x256.png", 
       MenuName = "PRIMAS SABATINAS Y DOMINICALES", 
       BenefitKind = "Subscripción", 
       Status = "Vigente" 
      })); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/OverTimeMoneyWhite256x256.png", 
       MenuName = "JORNADAS EXTRAORDINARIAS", 
       BenefitKind = "Subscripción", 
       Status = "Vigente" 
      })); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/VacationBonusWhite256x256.png", 
       MenuName = "PRIMA VACACIONAL", 
       BenefitKind = "Gratuito", 
       Status = "Vigente" 
      })); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/PecoWhite256x256.png", 
       MenuName = "PERMISOS ECONOMICOS", 
       BenefitKind = "Gratuito", 
       Status = "Vigente" 
      })); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/PunctualityBonusWhite256x256.png", 
       MenuName = "INCENTIVO PUNTUALIDAD Y ASISTENCIA", 
       BenefitKind = "Gratuito", 
       Status = "Vigente" 
      })); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/BonForSeniorityWhite256x256.png", 
       MenuName = "BONO DE ANTIGUEDAD", 
       BenefitKind = "Gratuito", 
       Status = "Vigente" 
      })); 
     AddNavMenuButtonItem(data, 
      new NavigationMenuButtonTemplateViewModel(new ButtonInfo 
      { 
       Symbol = @"ms-appx:///Assets/AssetsMainMenuPage/WageIncreaseWhite256x256.png", 
       MenuName = "RETROACTIVO SUELDO", 
       BenefitKind = "Gratuito", 
       Status = "Vigente" 
      })); 

     return data; 
    } 

    private static void AddNavMenuButtonItem(ObservableCollection<NavigationMenuButtonTemplateViewModel> data, NavigationMenuButtonTemplateViewModel item) 
    { 
     data.Add(item); 
    } 
} 

此外,如果你想對樣式屬性的編程,你應該這樣做像xaml.cs例如像這樣的:

public sealed partial class NavigationMenuButtonTemplate : UserControl 
{ 
    public NavigationMenuButtonTemplate() 
    { 
     this.InitializeComponent(); 
    } 

    private void NavigationMenuIconImage_PointerEntered(object sender, PointerRoutedEventArgs e) 
    { 
     var image = (Image)sender; 
     var bitmapImage = image.Source as BitmapImage; 
     var uri = bitmapImage?.UriSource; 
     var uriPath = uri?.AbsolutePath; 
     var newUriPath = [email protected]"ms-appx://{uriPath.Replace("White", "Black")}"; 
     image.Source = new BitmapImage(new Uri(newUriPath, UriKind.RelativeOrAbsolute)); 
    } 
} 

** SOLUTION 2號:** 另一個解決方案可以是使用具有依賴屬性用戶控件像這樣的:

<toolkitcontrols:Carousel x:Name="NavigationMenuCarouselPanel" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             Orientation="Horizontal" 
             ItemSource="{x:Bind ViewModel.MenuList}" 
             ItemMargin="25" 
             ItemDepth="160" 
             ItemRotationX="180" 
             ItemRotationY="25" 
             ItemRotationZ="0" 
             SelectedIndex="0" 
             Grid.Row="1"> 
       <toolkitcontrols:Carousel.EasingFunction> 
        <CubicEase EasingMode="EaseOut"/> 
       </toolkitcontrols:Carousel.EasingFunction>     
       <usercontrolvm:NavigationMenuButtonTemplate ButtonInfo="{x:Bind ViewModel.MenuList[0],Mode=OneWay}" 

NavigateToPageCommand =「{x:Bind ViewModel。NavigateToPageCommand}「/>

您必須創建一個NavigationMenuButtonTemplate.xaml.cs與依賴屬性像這樣的:

public sealed partial class NavigationMenuButtonTemplate : UserControl 
{ 
    public NavigationMenuButtonTemplate() 
    { 
     this.InitializeComponent(); 
    } 

    public DelegateCommand NavigateToPageCommand 
    { 
     get { return (DelegateCommand)GetValue(NavigateToPageCommandProperty); } 
     set { SetValue(NavigateToPageCommandProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for NavigateToPageCommand. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty NavigateToPageCommandProperty = 
     DependencyProperty.Register("NavigateToPageCommand", 
      typeof(DelegateCommand), 
      typeof(NavigationMenuButtonTemplate), 
      new PropertyMetadata(0)); 

    public ButtonInfo ButtonInfo 
    { 
     get { return (ButtonInfo)GetValue(ButtonInfoProperty); } 
     set { SetValue(ButtonInfoProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ButtonInfo. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ButtonInfoProperty = 
     DependencyProperty.Register("ButtonInfo", 
      typeof(ButtonInfo), 
      typeof(NavigationMenuButtonTemplate), 
      new PropertyMetadata(0));   
} 

我不喜歡這個解決辦法,因爲我不得不重複代碼在xaml文件,但它也是一個不錯的選擇

希望你喜歡我的回答我認爲它可以被我們許多人使用,它的適用於許多其他控制與你的無盡的想象力

+0

我只是創建一個用戶控件,它包裝一個按鈕並暴露像符號,ToPageType等依賴屬性。 –

+0

我的用戶控件包裝一個按鈕,但它不公開依賴屬性。你可以分享一個你的解決方案樣本,我會很感激你的想法學習和理解。 grettings哥們! –

+0

當然,我只是發佈了一個答案。 :) –

相關問題