0

我有一个下一个控件 - 日历。我从 nuget WPControls 得到的。我修改了它,因为我需要一个按钮 - 今天。

这是一个 XAML 代码:

<ControlTemplate x:Key="CalendarControlTemplate1"  TargetType="wpControls:Calendar">
        <ScrollViewer>
            <Grid Height="auto" Background="Black">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{TemplateBinding YearMonthLabel}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <Button 
                Content="&lt;" 
                Width="100" 
                Grid.Column="0"
                x:Name="PreviousMonthButton" 
                HorizontalAlignment="Left" 
                VerticalAlignment="Center" 
                Visibility="{Binding ShowNavigationButtons, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}"/>
                <Button 
                Content="&gt;" 
                Width="100" 
                Grid.Column="2" 
                x:Name="NextMonthButton" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Center" 
                Visibility="{Binding ShowNavigationButtons, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}"/>
                <Button Grid.Row="3" Grid.Column="1" x:Name="TodayButton" Content="{Binding Path=ButtonName}" HorizontalAlignment="Stretch" Height="100" VerticalAlignment="Bottom" Tap="Button_Tap" />

                <Grid Height="auto" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" x:Name="ItemsGrid">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{TemplateBinding Sunday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1"/>
                    <TextBlock Text="{TemplateBinding Monday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/>
                    <TextBlock Text="{TemplateBinding Tuesday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="3"/>
                    <TextBlock Text="{TemplateBinding Wednesday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="4"/>
                    <TextBlock Text="{TemplateBinding Thursday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="5"/>
                    <TextBlock Text="{TemplateBinding Friday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="6"/>
                    <TextBlock Text="{TemplateBinding Saturday}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="7"/>
                </Grid>

            </Grid>
        </ScrollViewer>
    </ControlTemplate>

我已经添加了这个东西:

<Button Grid.Row="3" Grid.Column="1" x:Name="TodayButton" Content="{Binding Path=ButtonName}" HorizontalAlignment="Stretch" Height="100" VerticalAlignment="Bottom" Tap="Button_Tap" />

我希望它改变它的内容,例如当我切换语言时的文本,所以我完成了这个绑定:

Content="{Binding Path=ButtonName}"

但它不起作用,为什么?以及如何解决?

这是我的 C# 代码:

string ButtonName;
public CalendarPage()
    {
        InitializeComponent();
        ButtonName = GS.translations["Today"];
    }
4

2 回答 2

1

您需要实现 INPC (INotifyPropertyChanged),然后触发 PropertyChanged 事件。这将更新按钮。

这正在进入 MVVM 领域,并且有许多这样的框架可以帮助您做到这一点。

格雷格

于 2013-08-26T22:23:31.487 回答
0

正如 Greg 所说,您需要有一个实现 INotifyPropertyChange 的数据上下文。还有另一种肮脏的方式。希望有效。1)改变你的代码是这样的

public string ButtonName{get;set;}
public CalendarPage()
    {
        InitializeComponent();
        ButtonName = GS.translations["Today"];
    }

2) 你的 Xaml 是这样的

Content="{Binding ElementName=YourUserControlName,Path=ButtonName}"
于 2013-08-28T10:39:53.907 回答