C# 在ContentTemplate中设置XAML Textblock内容
首先,我的代码: MainView.xaml:C# 在ContentTemplate中设置XAML Textblock内容,c#,wpf,xaml,windows-store-apps,C#,Wpf,Xaml,Windows Store Apps,首先,我的代码: MainView.xaml: <Grid Name="PropoCloud" VerticalAlignment="Bottom" Margin="0 0 0 80"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="FadeStates"> <VisualSt
<Grid Name="PropoCloud" VerticalAlignment="Bottom" Margin="0 0 0 80">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FadeStates">
<VisualState x:Name="FadeOut">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PropoCloud" Storyboard.TargetProperty="PropoCloud.Opacity" From="1" To="0" Duration="0:0:1"/>
</Storyboard>
</VisualState>
<VisualState x:Name="FadeIn">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PropoCloud" Storyboard.TargetProperty="PropoCloud.Opacity" From="0" To="1" Duration="0:0:2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<tut:TutorialAwareButton Name="PropoButton"
Command="{Binding CmdCreated}"
BorderThickness="0" VerticalAlignment="Bottom"
HorizontalAlignment="Left" Width="410" Height="200">
<tut:TutorialAwareButton.ContentTemplate>
<DataTemplate>
<Grid>
<Image Source="../Assets/propoCloud.png" Height="168" Width="370"/>
<Grid Width="215" Height="69" HorizontalAlignment="Right" Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Suggestion" FontFamily="Segoe UI Light" Foreground="{StaticResource BlueAppBackgroundThemeBrush}"
VerticalAlignment="Center"/>
<StackPanel Grid.Row="1" VerticalAlignment="Bottom">
<TextBlock x:Name="Exemples" TextWrapping="Wrap" FontSize="18" VerticalAlignment="Top" LineHeight="20"
Text=" test test test test tes tes tes testest stestestestests" MaxLines="2"
FontFamily="Segoe UI Light" Foreground="{StaticResource BlueAppBackgroundThemeBrush}"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
</DataTemplate>
</tut:TutorialAwareButton.ContentTemplate>
<tut:TutorialAwareButton.CommandParameter>
<cmd:NavigationCommandParameter TargetName="QuestionCreatingView"></cmd:NavigationCommandParameter>
</tut:TutorialAwareButton.CommandParameter>
</tut:TutorialAwareButton>
</Grid>
int count = 0;
private void Suggestionchange()
{
Exemples.Text = ExemplesQuestions[count];
count++;
if (count == 8) count = 0;
}
private void createExemple()
{
ExemplesQuestions = new List<string>();
ExemplesQuestions.Add("Test1");
ExemplesQuestions.Add("Test2");
ExemplesQuestions.Add("Test3");
ExemplesQuestions.Add("Test4");
ExemplesQuestions.Add("Test5");
ExemplesQuestions.Add("Test6");
ExemplesQuestions.Add("Test7");
ExemplesQuestions.Add("Test8");
}
MainView.xaml.cs:
<Grid Name="PropoCloud" VerticalAlignment="Bottom" Margin="0 0 0 80">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FadeStates">
<VisualState x:Name="FadeOut">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PropoCloud" Storyboard.TargetProperty="PropoCloud.Opacity" From="1" To="0" Duration="0:0:1"/>
</Storyboard>
</VisualState>
<VisualState x:Name="FadeIn">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PropoCloud" Storyboard.TargetProperty="PropoCloud.Opacity" From="0" To="1" Duration="0:0:2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<tut:TutorialAwareButton Name="PropoButton"
Command="{Binding CmdCreated}"
BorderThickness="0" VerticalAlignment="Bottom"
HorizontalAlignment="Left" Width="410" Height="200">
<tut:TutorialAwareButton.ContentTemplate>
<DataTemplate>
<Grid>
<Image Source="../Assets/propoCloud.png" Height="168" Width="370"/>
<Grid Width="215" Height="69" HorizontalAlignment="Right" Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Suggestion" FontFamily="Segoe UI Light" Foreground="{StaticResource BlueAppBackgroundThemeBrush}"
VerticalAlignment="Center"/>
<StackPanel Grid.Row="1" VerticalAlignment="Bottom">
<TextBlock x:Name="Exemples" TextWrapping="Wrap" FontSize="18" VerticalAlignment="Top" LineHeight="20"
Text=" test test test test tes tes tes testest stestestestests" MaxLines="2"
FontFamily="Segoe UI Light" Foreground="{StaticResource BlueAppBackgroundThemeBrush}"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
</DataTemplate>
</tut:TutorialAwareButton.ContentTemplate>
<tut:TutorialAwareButton.CommandParameter>
<cmd:NavigationCommandParameter TargetName="QuestionCreatingView"></cmd:NavigationCommandParameter>
</tut:TutorialAwareButton.CommandParameter>
</tut:TutorialAwareButton>
</Grid>
int count = 0;
private void Suggestionchange()
{
Exemples.Text = ExemplesQuestions[count];
count++;
if (count == 8) count = 0;
}
private void createExemple()
{
ExemplesQuestions = new List<string>();
ExemplesQuestions.Add("Test1");
ExemplesQuestions.Add("Test2");
ExemplesQuestions.Add("Test3");
ExemplesQuestions.Add("Test4");
ExemplesQuestions.Add("Test5");
ExemplesQuestions.Add("Test6");
ExemplesQuestions.Add("Test7");
ExemplesQuestions.Add("Test8");
}
int count=0;
私有void Suggestionchange()
{
Text=examplesquestions[count];
计数++;
如果(计数=8)计数=0;
}
私有void createExample()
{
exampleSquestions=新列表();
添加(“Test1”);
添加(“Test2”);
添加(“Test3”);
添加(“Test4”);
添加(“Test5”);
添加(“Test6”);
添加(“Test7”);
添加(“Test8”);
}
现在!我有一个计时器,每隔10秒它会尝试使用SuggestionChange()
更改文本。事实上,它只是说
当前上下文中不存在名称“examples”
我找不到如何更改它。您无法访问DataTemplate中定义的TextBlock。您可以为控件定义依赖项属性,并为显示数据定义访问该属性的权限。请参阅下面的代码
class TutorialAwareButton : Button
{
public string RandomString
{
get { return (string)GetValue(RandomStringProperty); }
set { SetValue(RandomStringProperty, value); }
}
public static readonly DependencyProperty RandomStringProperty =
DependencyProperty.Register("RandomString", typeof(string), typeof(TutorialAwareButton), new PropertyMetadata(string.Empty));
}
<local:TutorialAwareButton x:Name="PropoButton"
Command="{Binding CmdCreated}"
BorderThickness="0" VerticalAlignment="Bottom"
HorizontalAlignment="Left" Width="410" Height="200">
<local:TutorialAwareButton.ContentTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Bottom">
<TextBlock x:Name="Exemples" TextWrapping="Wrap" FontSize="18" VerticalAlignment="Top" LineHeight="20" Text="{Binding ElementName=PropoButton,Path=RandomString}" FontFamily="Segoe UI Light" />
</StackPanel>
</DataTemplate>
</local:TutorialAwareButton.ContentTemplate>
</local:TutorialAwareButton>
您只需要访问DataTemplate之外的内容,就可以访问您要查找的上下文。因此,在绑定基中使用FindAncestor或ElementName=之类的东西来访问外部视图就足够了。哦,拼写为“示例”;)它看起来不错,但我不能使用AncerstorType:在类型“relativesource”中找不到属性“ancestortype”是的,我在Windows应用商店8应用程序上,所以我尝试了:_Text=“{Binding RandomString,relativesource={StaticResource TutorialAwareButton}”,出现了调试器错误。您可以尝试。元素绑定。