如何在DataTemplate中使用WPF ContentControl的内容

时间:2014-11-18 20:30:12

标签: wpf contentcontrol

我使用内容控件复制了一些关于自定义按钮的资源。我将某些内容更改为<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContentControl},Path=Content}">对于dataTempalte

<DataTemplate x:Key="PriceDataTemplate" DataType="m:ClickTradeViewModel">
    <Button Command="{Binding ExecuteCommand}" Cursor="Hand">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter Content="{TemplateBinding Content}" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="DarkGray" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="#FF345C8B" />
                    </Trigger>
                    <DataTrigger Binding="{Binding IsExecuting}" Value="True">
                        <Setter Property="Background" Value="DimGray" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>

        <UserControl>
            <UserControl.Template>
                <ControlTemplate TargetType="UserControl">
                    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContentControl},Path=Content}"></TextBlock>
                </ControlTemplate>
            </UserControl.Template>
        </UserControl>
    </Button>
</DataTemplate>

对于实际的Button,它使用了

<ContentControl x:Name="AskContentControl" Grid.Column="2" 
                Margin="5,0,0,0"
                Content="{Binding QQ.Bid}" 
                ContentTemplate="{StaticResource PriceDataTemplate}"/>

我希望Content将使用double Bid的{​​{1}}方法来呈现内容,但内部没有显示任何内容(灰色)。在图中,左侧显示价格确实存在。

enter image description here

更新:我不确定发生了什么,但有一些更改tostring并设置了

<TextBlock Text="{Binding QQ.Ask}"></TextBlock>

问题是我必须为不同的属性多次明确设置<ContentControl x:Name="AskContentControl" Grid.Column="2" Margin="5,0,0,0" Content="{Binding}" ContentTemplate="{StaticResource PriceDataTemplate}"/> makes it work.

2 个答案:

答案 0 :(得分:1)

它不起作用,因为您使用与RelativeSource的Binding找到ContentControl,但UserControl也是ContentControl,所以它找到的实际上是UserControl,不是你想到的根ContentControl。在这种情况下,您可以将AncestorLevel指定为2(以查找第二个ContentControl):

<TextBlock Text="{Binding 
           RelativeSource={RelativeSource Mode=FindAncestor,
                           AncestorType=ContentControl, AncestorLevel=2},
           Path=Content}"></TextBlock>

然而,它并不是真正安全的,在这种情况下,隐式DataContext实际上是您为Content设置的ContentControl(此DataContext从{{1}向下流动通过UserControl的模板)。所以Binding可以很简单:

DataTemplate

注意我认为您一直将ContentControl的内容设置为<TextBlock Text="{Binding}"></TextBlock>

答案 1 :(得分:0)

这是一个完整的解决方案......我迟到了,但也许可以帮助其他人?

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ParametricStudyAnalysis.ScopeSelection.Special"
             xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" x:Class="ParametricStudyAnalysis.ScopeSelection.Special.UserControlAddSpecialSignal"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <local:UserControlAddSpecialSignalModel></local:UserControlAddSpecialSignalModel>
    </UserControl.DataContext>

    <UserControl.Resources>
        <DataTemplate DataType="{x:Type local:UserControlSpecialSignalTtrModel}">
            <local:UserControlSpecialSignalTtr/>
        </DataTemplate>     
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>


        <GroupBox Header="Signal type" Grid.Row="0" Padding="5">
            <xcdg:DataGridControl Name="DataGrid" SelectionMode="Single" ItemsSource="{Binding SpecialSignalEntries}"
                              SelectedItem="{Binding SpecialSignalEntrySelected}" Height="200">
            <xcdg:DataGridControl.Columns>
                <xcdg:Column FieldName="Name" Title="Type of special signal" ReadOnly="True"></xcdg:Column>
            </xcdg:DataGridControl.Columns>
        </xcdg:DataGridControl>
        </GroupBox>

        <GroupBox Header="Parameters" Grid.Row="1" Margin="0,3,0,0" Padding="5">
            <ContentControl Name="MyContentControl" 
                            DataContext="{Binding SpecialSignalEntrySelected, Mode=OneWay}" 
                            Content="{Binding SignalProviderSpecial}">
            </ContentControl>
        </GroupBox>
    </Grid>
</UserControl>