gpt4 book ai didi

c# - 银光。将样式项绑定(bind)到数据上下文

转载 作者:行者123 更新时间:2023-11-30 21:20:18 26 4
gpt4 key购买 nike

我有一个带有自定义样式的按钮,我想将项目样式设置为按钮数据上下文。知道下面的代码有什么问题吗?

这是我所做的:

   <UserControl.Resources>
<Style x:Key="ButtonStyle1" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding ContextText}" DataContext="{TemplateBinding DataContext}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Button" Height="131" Width="103" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click"/>
<Button x:Name="button" Style="{StaticResource ButtonStyle1}" Margin="165,86,0,0"/>

</Grid>

C#代码

 public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
button.DataContext=new Test(){ ContextText="TextFromContext"};
button.UpdateLayout();
}
}
public class Test
{
public String ContextText{get;set;}
}

最佳答案

嗯..这就是你要找的:-

<Button x:Name="button" Content="{Binding ContextText}" Style="{StaticResource ButtonStyle1}" Margin="165,86,0,0"/> 

差点不好意思提交了,好像101了

关于c# - 银光。将样式项绑定(bind)到数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3351528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com