MVVMCross WinStore将项添加到使用DataTemplate定义的XAML

时间:2014-05-17 04:47:15

标签: xaml windows-runtime mvvmcross

所以我在视图模型中定义了一些项目,然后我想在XAML中添加到我的视图中。由于这些项目很多,我试图通过在视图模型中创建为自定义类定义的datatemplate来减少一些冗余,然后将它们嵌套在XAML中。到目前为止我看起来像这样。

    <Page.Resources>

    <DataTemplate x:Name="CommandCenterTemplate"  >
        <Grid d:DataContext="{d:DesignInstance core:ComboBoxCommandCenter}">
            <Grid.RowDefinitions >
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="100"/>
            </Grid.ColumnDefinitions>

            <Button Width="150" Content="{Binding Title}" Command="{Binding OnClickCommand}"/>
            <ComboBox Grid.Column="1" ItemsSource="{Binding Options}" SelectedValue="{Binding Selected,Mode=TwoWay}"/>

        </Grid>
    </DataTemplate>
    <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
    <x:String x:Key="AppName">Combat</x:String>
</Page.Resources>

以前曾在XAML中逐行逐条列出这些内容,如下所示:

<StackPanel Orientation ="Horizontal">
                <Button Width="150" Content="Skills" Command="{Binding SkillCommand}"></Button>
                <ComboBox ItemsSource="{Binding SkillOptions}" SelectedValue="{Binding SkillSelected,Mode=TwoWay}"></ComboBox>
            </StackPanel>

在View-Model中定义的类看起来像这样:

public class ComboBoxCommandCenter : INotifyPropertyChanged 
{
    private IEnumerable<string> _options;
    private string _title;

    public ComboBoxCommandCenter(string title,IEnumerable<string> options, Action<string> action)
    {
        _options = options;
        OnClickAction = action;

    }
    private Action<string> OnClickAction { get; set; }
    public string Selected { get; set; }

    public IEnumerable<string> Options
    {
        get { return _options; }
    }

    public ICommand OnClickCommand
    {
        get { return new MvxCommand(() => OnClickAction(Selected)); }
    }

    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            OnPropertyChanged();
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

所以我希望能够在视图模型中用实例化的类实例替换堆栈面板对象。

public class CombatViewModel : BaseViewModel
{
       public ComboBoxCommandCenter SaveCenter { get; private set; }

public CombatViewModel(ICharacterService character)             :基地(人物)         {             SaveCenter = new ComboBoxCommandCenter(&#34; Save&#34;,MyCharacter.Actions.SaveOptions(),MyCharacter.Actions.Save);         }     }

那么有没有办法在XAML中放置一些类似

的代码
<core:ViewModel.SaveCenter />

1 个答案:

答案 0 :(得分:2)

如果我理解正确,那么就没有ComboBoxCommandCenters列表,只有一个。你的问题是如何展示这一个ComboBoxCommandCenter。在这种情况下,您可以使用:

<ContentControl 
    ContentTemplate="{StaticResource CommandCenterTemplate}" 
    Content="{Binding pathToYourViewModel_SaveCenter}"
    />

更新:回答评论中的问题:

  

我有多个c omboboxes要添加。当我声明内容确实或者xaml可以根据其类别拉出模板时?这几乎可能需要单独编写的控件,或者简单地在内联xaml中定义它我想知道。

比创造一个新的控件更简单:

您可以设置ContentTemplateSelector,而不是设置ContentTemplate。要使用ContentTemplateSelector在代码中选择模板,您必须从DataTemplateSelector继承并通过重写SelectTemplate()来添加您的选择逻辑。

更优雅的解决方案是&#34;隐含的DataTemplates&#34;这使得XAML根据数据类型选择默认的DataTemplate。但这仅适用于WPF。在Windows应用商店应用中,此类行为仅适用于样式(通过TargetType)。