【问题标题】:Button text template data binding is not working按钮文本模板数据绑定不起作用
【发布时间】:2013-12-29 00:51:56
【问题描述】:

我正在开发我的第一个 Windows 8 应用程序,在一个页面中,我尝试在页面加载时使用最新的时间停止更新按钮文本。我定义了我的 xaml 和代码隐藏,如下所示:

我正在使用数据绑定来更新按钮文本,但它没有按预期工作:

MainPage.xaml

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Button HorizontalAlignment="Left" Margin="333,284,0,0" VerticalAlignment="Top" Height="69" Width="162">
        <Button.Resources>
            <DataTemplate x:Key="DataTemplate1">
                <Grid>
                    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ButtonText}"  VerticalAlignment="Top" Foreground="#FFFF6800" Height="34" Margin="-30,0,-22,-14" Width="115"/>
                </Grid>
            </DataTemplate>
        </Button.Resources>
        <Button.ContentTemplate>
            <StaticResource ResourceKey="DataTemplate1"/>
        </Button.ContentTemplate>
    </Button>

</Grid>

MainPage.xaml.cs

public StatsClass Stats { get; private set; }

    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = Stats;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        UpdateButton();
    }

    private void UpdateButton()
    {
        if (Stats == null)
            Stats = new StatsClass();

        Stats.ButtonText = DateTime.Now.ToString();
    }

StatsClass.cs

public class StatsClass : INotifyPropertyChanged
{
    private string _buttonText;
    public string ButtonText
    {
        get
        {
            return _buttonText;
        }

        set
        {
            _buttonText = value;
            OnPropertyChanged("ButtonText");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

【问题讨论】:

  • 我认为您还必须在主页中调用属性更改
  • @SandeepChauhan 不,你不会

标签: c# xaml data-binding windows-8


【解决方案1】:

您已经设置了两次按钮的内容,一次使用Content="Button",另一次使用.Button.ContentTemplate。你可以有:

<Button HorizontalAlignment="Left" Margin="333,284,0,0" VerticalAlignment="Top" Height="69" Width="162">
    <Grid>
         <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ButtonText}"  VerticalAlignment="Top" Foreground="#FFFF6800" Height="34" Margin="-30,0,-22,-14" Width="115"/>
    </Grid>
</Button>

【讨论】:

  • 抱歉,这是问题中的拼写错误,我更改了它 - 但它没有触发绑定。
  • 您是否在属性设置器中设置了断点来验证它是否被设置,您是否监控了 Debug 输出中的绑定错误?
  • 感谢您提供有关绑定错误的提示。实际上,它有一个错误 - 修复是在 Binding 定义中添加 ElementName。
  • 好的,我猜那是因为您使用的是模板,否则您不需要,例如在我上面的代码中,作为继承的 DataContext 并且您已将其设置为 StatsClass 的实例
【解决方案2】:

我昨天在 DataTemplate 中使用绑定时遇到了类似的问题。我猜您在调试输出中也有绑定错误。 我使用这样的相对来源解决了它:

<TextBlock Text={Binding DataContext.ButtonText, 
           RelativeSource={RelativeSource FindAncestor, AncestorType=*YourControl*}}"/>

模板无法直接访问数据上下文。通过使用相对源,您可以绑定到它的属性。

【讨论】:

    猜你喜欢
    • 2015-10-25
    • 2011-11-21
    • 2021-02-08
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多