为什么不赢得我的模板的TextBox绑定

时间:2014-12-17 15:36:07

标签: c# wpf xaml mvvm

我正在尝试做一些绑定。在大多数情况下,我的MVVM应用程序运行正常但我现在想要使用模板。在这种情况下,我使用的XCeed图表显示SeriesTemplate

我的代码是:

  <UserControl
 xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
          x:Class="MyApp.AppUi.View.Graph.GraphView"
         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:converter="clr-namespace:MyApp.AppUi.Converters" 
         >

<Grid>

    <Grid.Resources>

        <converter:MyIntConverter x:Key="MyIntConverter" />

        <DataTemplate x:Key="MyLabelTemplate">
            <TextBlock Text="{Binding Path=Text, Converter={StaticResource MyIntConverter}}" />
        </DataTemplate>

        <!--give it a name,                             Bind to above,        choose property -->
        <CollectionViewSource x:Key="GraphDataCollection" Source="{Binding GraphDataList}" />


        <Style  x:Key="TextBoxTextStyle" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="Comic Sans MS"/>
            <Setter Property="FontSize" Value="12"/>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="FontSize" To="19" Duration="0:0:0.4"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>

                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="FontSize" To="12" Duration="0:0:0.4" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" 
                         Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
                            <Rectangle RadiusX="5" RadiusY="5" >
                                <Rectangle.Triggers>
                                    <EventTrigger RoutedEvent="Mouse.MouseEnter">
                                        <EventTrigger.Actions>
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.8" Duration="0:0:0.4"/>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger.Actions>
                                    </EventTrigger>

                                    <EventTrigger RoutedEvent="Mouse.MouseLeave">
                                        <EventTrigger.Actions>
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.4" />
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger.Actions>
                                    </EventTrigger>
                                </Rectangle.Triggers>

                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                        <GradientStop Color="#FFffcf26" Offset="0.0" />
                                        <GradientStop Color="#FFff7f04" Offset="1.0" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter VerticalAlignment="Center" Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        <DataTemplate x:Key="SeriesTemplate">
            <Button>
                <StackPanel>
                     <TextBlock Text="{Binding GraphData.ShortDate}" Style="{StaticResource TextBoxTextStyle}" />
                </StackPanel>
            </Button>
        </DataTemplate>
    </Grid.Resources>

    <GroupBox Header="{Binding Title}">
        <GroupBox.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0" >
                <GradientStop Offset="0" Color="#FF9d9d9d" />
                <GradientStop Offset="1" Color="#FF666666" />
            </LinearGradientBrush>
        </GroupBox.Background>

        <xctk:Chart Height="400" Width="400" ShowLegend="False">

           <xctk:Chart.Areas>
            <xctk:Area >
                <xctk:Area.XAxis>

                    <xctk:Axis Title="Date" 
                                   GraduationMode="Manual" 
                                   LabelsType="DateTime"
                                   ScaleMode="Automatic"
                                   TitleMargin="10"
                                   AxisLabelsLayout="ShowAll"
                                   ShowArrow="False"
                                   ShowAxis="True"
                                   ShowGridLines="True"
                                   ShowTicks="True"
                                   ShowTickLabels="True"
                                   ShowAxisLabel="True"
                                   Reversed="False"
                                   />
                </xctk:Area.XAxis>
                <xctk:Area.YAxis>
                    <xctk:Axis Title="Google position"  
                                   ScaleMode="Manual"
                                   TitleMargin="10"
                                   AxisLabelsLayout="ShowAll"
                                   ShowArrow="False"
                                   ShowAxis="True"
                                   ShowGridLines="True"
                                   CustomRangeStart="1.00"
                                   CustomRangeEnd="111.00"
                                   ShowTicks="True"
                                   ShowTickLabels="True"
                                   ShowAxisLabel="True"
                                   Reversed="False" 
                                   LabelTemplate="{StaticResource MyLabelTemplate}"
                                   />
                </xctk:Area.YAxis>

                <xctk:Area.Series>
                        <xctk:Series DataPointsSource="{Binding Source={StaticResource GraphDataCollection}}"
                              Template="{StaticResource SeriesTemplate}"
                              ShowPointsInLegend="true">
                        <xctk:Series.DataPointBindings>
                            <xctk:BindingInfo PropertyName="Y">
                                <xctk:BindingInfo.Binding>
                                    <Binding Path="Position"/>
                                </xctk:BindingInfo.Binding>
                            </xctk:BindingInfo>
                            <xctk:BindingInfo PropertyName="X">
                                <xctk:BindingInfo.Binding>
                                        <Binding Path="Date"/>
                                </xctk:BindingInfo.Binding>
                            </xctk:BindingInfo>
                            <xctk:BindingInfo PropertyName="Label">
                                <xctk:BindingInfo.Binding>
                                    <Binding Path="ShortDate"/>
                                </xctk:BindingInfo.Binding>
                            </xctk:BindingInfo>
                        </xctk:Series.DataPointBindings>
                    </xctk:Series>
                </xctk:Area.Series>
            </xctk:Area>
        </xctk:Chart.Areas>
    </xctk:Chart>
    </GroupBox>


</Grid>

和我的ViewModel

public class GraphViewModel : BaseViewModel
{
    public GraphViewModel(List<GraphData> data, string title )
    {
        this.GraphDataList = data;
        this.Title = title;
    }

    public string Title { get; private set; }
    public List<GraphData> GraphDataList { get; private set; }
}

DataContext在资源字典中设置

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:graphView="clr-namespace:MyApp.AppUi.View.Graph"
                xmlns:graphViewModel="clr-namespace:MyApp.AppUi.ViewModel.Graph"
                xmlns:converter="clr-namespace:MyApp.AppUi.Converters"
                >

<DataTemplate DataType="{x:Type graphViewModel:GraphViewModel}">
    <graphView:GraphView />
</DataTemplate>

图表会根据需要显示,但SeriesTemplate不会绑定。在输出窗口中有一条错误消息,我理解消息的内容,但不知道如何修复它。

错误消息是

  

System.Windows.Data错误:40:BindingExpression路径错误:'object'''ColumnPrimitiveInfo'(HashCode = 39288004)'上找不到'ShortDate'属性。 BindingExpression:路径= ShortDate; DataItem ='ColumnPrimitiveInfo'(HashCode = 39288004); target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')

如何让绑定适用于

  <DataTemplate x:Key="SeriesTemplate">
            <Button x:Name="Bar">
                <StackPanel>                       
                    <TextBlock Text="{Binding GraphData.ShortDate}" Style="{StaticResource TextBoxTextStyle}" /> <!--THIS IS WHERE THE FAULT IS -->
                </StackPanel>
            </Button>
        </DataTemplate>

XCeed有working demo源代码,如果您下载应用程序,请点击图表 - &gt;造型 - &gt; Column Series你可以看到我的代码非常相似。有工作,但我的不是,我不明白为什么。请注意,图表本身显示,它只是文本框没有显示。但是,如果我不使用绑定,只需使用Text="SomeWords",那么它可以正常工作

3 个答案:

答案 0 :(得分:1)

经过几个小时的巫术编程(我只是随意尝试不同的东西),我找到了解决方案。

 <TextBlock Text="{Binding Path=Content.ShortDate}" Style="{StaticResource TextBoxTextStyle}" />

我认为这是因为(参见OP代码中的Button Setter)

 <ContentPresenter VerticalAlignment="Center" Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>

答案 1 :(得分:0)

试试这个:

    <DataTemplate x:Key="SeriesTemplate">
        <Button>
            <StackPanel>
                 <TextBlock Text="{Binding Path=ShortDate}" Style="{StaticResource TextBoxTextStyle}" />
            </StackPanel>
        </Button>
    </DataTemplate>

答案 2 :(得分:0)

从这里查看文档on the XCeed documentation site看来,您正在为绑定添加额外的间接层。尝试直接绑定到GraphDataList

<xctk:Series DataPointsSource="{Binding Source={StaticResource GraphDataList}}"
                          Template="{StaticResource SeriesTemplate}"
                          ShowPointsInLegend="true">

然后在系列

<DataTemplate x:Key="SeriesTemplate">
    <Button>
        <StackPanel>
             <TextBlock Text="{Binding Path=ShortDate}" Style="{StaticResource TextBoxTextStyle}" />
        </StackPanel>
    </Button>
</DataTemplate>