c# - 绑定(bind)到 Windows Phone 上 DataTemplate 的元素

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

简单明了:我需要绑定(bind) ContextMenu 的一些属性DataTemplate 中其父项的属性的项目.

我找不到访问它的方法,因为 ElementName不起作用并且 RelativeSource允许我只使用 SelfTemplatedParent .

这是我的代码:

<telerikPrimitives:RadDataBoundListBox Grid.Row="2"
                                       Grid.Column="0"
                                       Grid.ColumnSpan="3"
                                       ItemsSource="{Binding Transfers.Keys, Source={StaticResource TransfersManager}, Mode=OneWay}">
                <telerikPrimitives:RadDataBoundListBox.ItemTemplate>
                    <DataTemplate>
                        <toolkit:TransferControl x:Name="TransferControl"
                            Header="{Binding Converter={StaticResource TransferMonitorToDocumentTitleConverter}}"
                            IsContextMenuEnabled="False"
                            Icon="{Binding Converter={StaticResource TransferMonitorToDocumentIconUriConverter}}"
                            AutoHide="False"
                            Monitor="{Binding}"
                            Language="it-IT"
                            StatusTextBrush="Black"
                            Foreground="Black">
                            <toolkit:TransferControl.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <toolkit:ContextMenuService.ContextMenu>
                                            <toolkit:ContextMenu>
                                                <toolkit:MenuItem Header="item1"
                                                                  IsEnabled="{Binding Monitor Property in the TransferControl object}"
                                                                  />
                                                <toolkit:MenuItem Header="item2"/>
                                            </toolkit:ContextMenu>
                                        </toolkit:ContextMenuService.ContextMenu>
                                        <Rectangle Fill="Transparent" Height="30"/>
                                        <ContentControl Content="{Binding}"
                                                        HorizontalAlignment="Left"
                                                        VerticalAlignment="Center"
                                                        Foreground="Black" />                                        
                     </StackPanel>
                 </DataTemplate>
             </toolkit:TransferControl.HeaderTemplate>
         </toolkit:TransferControl>
     </DataTemplate>
</telerikPrimitives:RadDataBoundListBox.ItemTemplate>

我想要绑定(bind)的是:

<toolkit:MenuItem Header="item1" 
                  IsEnabled="{Binding Monitor Property in the TransferControl object}"
                  />

我想将它绑定(bind)到 Monitor <toolkit:TransferControl x:Name="TransferControl" ... />的属性(property)对象。

最佳答案

我只需创建一个新的用户控件来保存列表框的内容就可以解决这个问题。我使用了长列表选择器。这是一个有效的解决方案。

首先是页面的 XAML:

<phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Items}">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <local:WindowsPhoneControl/>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

然后是用户控件。这没什么特别的,只是一个包装器。

    <UserControl x:Class="PivotApp1.WindowsPhoneControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <phone:LongListSelector x:Name="TransferControl" Margin="0,0,-12,0"
                                ItemsSource="{Binding Items}"
                                toolkit:TiltEffect.IsTiltEnabled="True">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu>
                                <toolkit:MenuItem Header="item1"
                                                  IsEnabled="{Binding DataContext.Monitor, ElementName=TransferControl}"/>
                                <toolkit:MenuItem Header="item2"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <StackPanel Margin="0,0,0,17">
                            <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>
</UserControl>

使用此解决方案,渲染列表时会触发 Monitor 属性。打开菜单时不会被击中。当然,如果您触发监视器的 propertyChanged,它将再次获取该值。

关于c# - 绑定(bind)到 Windows Phone 上 DataTemplate 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23845617/

相关文章:

c# - 为什么从投影创建的整数变量不能递增?

c# - MaxConcurrentRequestsPerCPU 和 MaxConcurrentThreadsPerCPU 没有按照记录工作?

.net - 单独程序集中的 ResourceDictionary

wpf - 使用 MVVM 的 WPF 中 ItemSource 的绑定(bind)/DataContext 问题

windows-phone-8 - 适用于 Windows Phone 8 的企业应用程序分发 - 是否需要旁载许可证?

c# - 从不同项目访问 Windows Phone 上的 Azure 移动服务

c# - 图标到 ImageSource 的运行时转换

wpf - 带填充和描边的线条

c# - 带有 HtmlAgilityPack 的 HTML Linq,或 P​​CL 中的替代方案

C#:I/O 繁重的多线程性能不佳