簡體   English   中英

如何將WPF ContentControl的內容與DataTemplate一起使用

[英]How to use WPF ContentControl 's content with DataTemplate

我使用內容控件復制了一些有關自定義按鈕的資源。 然后我將其更改為<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 Bidtostring方法來呈現內容,但是它內部什么都沒有顯示(灰色)。 在圖中左側顯示價格確實存在。

在此處輸入圖片說明

更新 :我不確定發生了什么,但是做了一些更改<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. 

問題是我不得不為不同的屬性多次顯式設置PriceDataTemplate

它不起作用,因為您正在使用帶有RelativeSource的綁定來查找ContentControlUserControl也是ContentControl ,因此找到的實際上是UserControl ,而不是您認為的根ContentControl 在這種情況下,您可以將一些AncestorLevel指定為2 (以找到第二個ContentControl ):

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

但是,這並不是真正安全的,在這種情況下,隱式DataContext實際上是您為ContentControl設置的Content (此DataContextDataTemplate向下流經UserControl的模板)。 因此,綁定可以像下面這樣簡單:

<TextBlock Text="{Binding}"></TextBlock>

請注意,我假設您將ContentControl的Content設置為{Binding QQ.Bid}

這是一個完整的工作解決方案...我來晚了,但也許可以幫助其他人嗎?

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM