У меня проблема с DataTemplates en ContentControl. Мое дело очень специфично.XAML ContentControl Data Binding не работает
XAML страницы:
<Page
x:Class="Questionnaires.QuestionPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Questionnaires"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:selectors="using:Questionnaires.TemplateSelectors"
xmlns:interop="using:Windows.UI.Xaml.Interop"
mc:Ignorable="d">
<Page.Resources>
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<selectors:QuestionTypeItemTemplateSelector x:Key="QuestionTypeSelector" />
<selectors:AnswerTypeItemTemplateSelector x:Key="AnswerTypeSelector"/>
<DataTemplate x:Key="SingleSelectQuestionItemTemplate">
<Grid Margin="10">
<RadioButton HorizontalAlignment="Left" VerticalAlignment="Center"
IsChecked="{Binding IsChecked, Mode=TwoWay}"
Width="600" Height="Auto" GroupName="groupName">
<RadioButton.Content>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap"/>
</RadioButton.Content>
</RadioButton>
</Grid>
</DataTemplate>
<DataTemplate x:Key="FreeQuestionItemTemplate">
<Grid Margin="10">
<ContentPresenter Content="{Binding}" ContentTemplateSelector="{StaticResource AnswerTypeSelector}" />
<!--
<GridView ItemsSource="{Binding}" SelectionMode="None">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
-->
</Grid>
</DataTemplate>
<DataTemplate x:Key="MultiSelectQuestionItemTemplate">
<Grid Margin="10">
<CheckBox HorizontalAlignment="Left" VerticalAlignment="Center"
IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="600">
<CheckBox.Content>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap"/>
</CheckBox.Content>
</CheckBox>
</Grid>
</DataTemplate>
<DataTemplate x:Key="LabelAnswerItemTemplate">
</DataTemplate>
<DataTemplate x:Key="TextAreaAnswerItemTemplate">
</DataTemplate>
<DataTemplate x:Key="TextFieldAnswerItemTemplate">
</DataTemplate>
<DataTemplate x:Key="DateAnswerItemTemplate">
</DataTemplate>
<DataTemplate x:Key="SliderAnswerItemTemplate">
<Grid Margin="10">
<Slider Width="600" Minimum="0" Maximum="100" Value="25" />
</Grid>
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.Background>
<ImageBrush Stretch="None" ImageSource="Assets/IS_Bol_White.png"/>
</Grid.Background>
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="108*"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="235*"/>
<ColumnDefinition Width="1011*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="pageTitle" Text="{Binding Path=Assignment.Definition.Name}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1"
IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40" Grid.ColumnSpan="2" FontSize="48"/>
<Image Grid.Column="2" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,30,0" Width="252" Height="71" Source="ms-appx:///Assets\innovationstudio.png" />
<Grid Grid.Column="2" Grid.Row="1" Margin="3,0,358,0" Width="650">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ProgressBar Grid.Row ="0" Maximum="1" Value="{Binding Progress}" IsIndeterminate="False" Grid.ColumnSpan="2" Foreground="Black"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Margin="10 20" TextAlignment="Left" HorizontalAlignment="Left" Style="{StaticResource SubheaderTextBlockStyle}"
Text="{Binding CurrentQuestion.Text}" />
</Grid>
<GridView Grid.Column="2" Grid.Row="2" ItemsSource="{Binding CurrentQuestion.PossibleAnswers}"
ItemTemplateSelector="{StaticResource QuestionTypeSelector}" SelectionMode="None">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
<Grid Grid.Column="2" Grid.Row="3" Margin="3,0,358,0" Width="650">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="btn_Previous" Grid.Column="0" HorizontalAlignment="Left" Content="Vorige" Height="59" Width="175" IsEnabled="{Binding IsPreviousButtonEnabled}" Click="btnPrevious_Click"/>
<Button x:Name="btn_Next" Grid.Column="1" HorizontalAlignment="Right" Content="Volgende" Height="59" Width="175" IsEnabled="{Binding IsNextButtonEnabled}" Click="btnNext_Click"/>
</Grid>
</Grid>
</Page>
Я использую ItemTemplateSelector указать, какой тип вопроса он будет. Тип QuestionType находится в объекте Возможный ответ. Моя ItemTemplateSelector выглядит следующим образом:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Questionnaires.Models;
using Questionnaires.ViewModels;
namespace Questionnaires.TemplateSelectors
{
public class QuestionTypeItemTemplateSelector : DataTemplateSelector
{
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
switch (((PossibleAnswer)(item)).QuestionType)
{
case "FREE":
return
((Page) ((Frame) Window.Current.Content).Content).Resources["FreeQuestionItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
case "SINGLE_SELECT":
return
((Page) ((Frame) Window.Current.Content).Content).Resources["SingleSelectQuestionItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
case "MULTI_SELECT":
return
((Page) ((Frame) Window.Current.Content).Content).Resources["MultiSelectQuestionItemTemplate"]
as Windows.UI.Xaml.DataTemplate;
case "DROPDOWN":
return
((Page) ((Frame) Window.Current.Content).Content).Resources["DropdownQuestionItemTemplate"]
as Windows.UI.Xaml.DataTemplate;
default:
return null;
}
}
}
}
Таким образом, я указать, какие DataTemplate должно быть показано на моей странице. В настоящее время я работаю над БЕСПЛАТНЫМИ вопросами, поэтому сейчас я говорю конкретно об одном шаблоне. Шаблон на моем XAML выглядит следующим образом:
<Page.Resources>
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<selectors:QuestionTypeItemTemplateSelector x:Key="QuestionTypeSelector" />
<selectors:AnswerTypeItemTemplateSelector x:Key="AnswerTypeSelector"/>
<DataTemplate x:Key="FreeQuestionItemTemplate">
<Grid Margin="10">
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource AnswerTypeSelector}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="SliderAnswerItemTemplate">
<Grid Margin="10">
<Slider Width="600" Minimum="0" Maximum="100" Value="25" />
</Grid>
</DataTemplate>
</Page.Resources>
Чтобы сделать вещи немного яснее: есть разница между QuestionType и PossibleAnswerType. QuestionType может быть:
FREE SINGLE_SELECT MULTI_SELECT DROPDOWN Таблица
PossibleAnswerType может быть:
LABEL TEXT_AREA TEXT_FIELD ДАТА Slider
Я пытаюсь получить PossibleAnswerType из мой вариант ответа на FreeQuestionTemplate. Я использую ContentControl, чтобы сделать это, но привязка моего возможного ответа не работает.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Questionnaires.Models;
namespace Questionnaires.TemplateSelectors
{
public class AnswerTypeItemTemplateSelector : DataTemplateSelector
{
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
switch (((PossibleAnswer) (item)).PossibleAnswerType)
{
case "LABEL":
return
((Page)((Frame)Window.Current.Content).Content).Resources["LabelAnswerItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
case "TEXT_AREA":
return
((Page)((Frame)Window.Current.Content).Content).Resources["TextAreaAnswerItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
case "TEXT_FIELD":
return
((Page)((Frame)Window.Current.Content).Content).Resources["TextFieldAnswerItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
case "DATE":
return
((Page)((Frame)Window.Current.Content).Content).Resources["DateAnswerItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
case "SLIDER":
return
((Page)((Frame)Window.Current.Content).Content).Resources["SliderAnswerItemTemplate"]
as
Windows.UI.Xaml.DataTemplate;
default:
return null;
}
}
}
}
В моем типе ответаTypeItemTemplateSelector объект объекта всегда равен нулю. Кто-нибудь дал мне несколько советов о том, как это сделать?
С уважением!
Попробуйте прочитать свой вопрос * перед тем, как вы его опубликуете в следующий раз ... вам не хватало куска кода. Я переформатировал его, чтобы его можно было увидеть сейчас. – Sheridan
Вы пробовали простую проверку «null»? 'if (object == null) return;' – Sheridan
Дело в том, что мой элемент должен быть Возможным ответом :) Нулевая проверка здесь не имеет значения.Элемент должен содержать данные на этом этапе. – TurboTheunis