简体   繁体   English

在ControlsTemplate中绑定

[英]Binding in ControlsTemplate

i have a next control - Calendar. 我有一个下一个控件-日历。 Which i got from nuget WPControls. 我从nuget WPControls获得。 I modified it, because i needed a button - Today. 我修改了它,因为我需要一个按钮-今天。

Here is a XAML code: 这是一个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>

I have added this thing: 我添加了这个东西:

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

I want it to change it's content, eg text when i switch languages, so i have done this binding: 我希望它更改其内容,例如,当我切换语言时是文本,因此我完成了此绑定:

Content="{Binding Path=ButtonName}"

But it doesn't work, why? 但这不起作用,为什么呢? and how to fix it? 以及如何解决?

Here is my C# code: 这是我的C#代码:

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

You need to implement INPC (INotifyPropertyChanged) and then fire the PropertyChanged event. 您需要实现INPC(INotifyPropertyChanged),然后触发PropertyChanged事件。 This will update the button. 这将更新按钮。

This is getting into the realm of MVVM and there are many such frameworks to help you do this. 这已经进入了MVVM领域,有许多这样的框架可以帮助您做到这一点。

Greg 格雷格

As Greg said, you need to have a data context which implements INotifyPropertyChange. 正如Greg所说,您需要具有实现INotifyPropertyChange的数据上下文。 There's another dirty way.Hope that works. 还有另一种肮脏的方式,希望能奏效。 1) Change your code something like this 1)像这样更改代码

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

2) And your Xaml somthing like this 2)你的Xaml这样

Content="{Binding ElementName=YourUserControlName,Path=ButtonName}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM