2014-12-17 4 views
1

Я пытаюсь сделать привязку. По большей части мое приложение MVVM работает нормально, но теперь я хочу использовать шаблон. В этом случае я использую график XCeed, который выставляет SeriesTemplate.Почему не привязывается к TextBox моего шаблона

Мой код:

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

на диаграмме отображаются как желание d, но SeriesTemplate не связывается. В окне вывода есть сообщение об ошибке, которое я понимаю, что такое сообщение, но не как его исправить.

Сообщение об ошибке

System.Windows.Data Error: 40 : BindingExpression path error: 'ShortDate' property not found on 'object' ''ColumnPrimitiveInfo' (HashCode=39288004)'. BindingExpression:Path=ShortDate; DataItem='ColumnPrimitiveInfo' (HashCode=39288004); target element is 'TextBlock' (Name=''); target property is 'Text' (type '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 с исходным кодом, если вы загрузите приложение, нажмите на графиках -> Styling -> Column Series вы можете видеть, что мой код очень похож. Есть работы, но у меня нет, и я не понимаю, почему. Обратите внимание: сам график отображается, это только текстовое поле не отображается. Однако, если я не использую привязки, и просто использовать Text="SomeWords" то он работает отлично

+0

Где ваша собственность GraphData? В вашей модели ViewModel или это свойство xceed? Не могли бы вы предоставить нам ссылку на документацию по компоненту, который вы используете? Не удалось найти его на веб-сайте. http://doc.xceedsoft.com/products/XceedChart/ – nkoniishvt

+0

@nkoniishvt вы можете загрузить свою демоверсию (которая показывает исходный код) из http://xceed.com/WPF_Toolkit_Demo.html - Когда она загружается, в диаграммах расширяется стилизация и нажмите «Column Series». Отсюда, справа, вы можете увидеть код XAML (который работает). Я в значительной степени копировал и вставлял (он работал), затем менял несколько вещей и разбил его. Я не вижу, что я сломал – MyDaftQuestions

+0

Просьба опубликовать ваш класс ViewModel и опубликовать полный xaml – nkoniishvt

ответ

0

Попробуйте это:

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

К сожалению, это не сработало – MyDaftQuestions

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

К сожалению, для этого необходим дополнительный слой. Думаю, потому что граф не является частью визуального дерева и как таковой, требует этого дополнения, чтобы сделать работу по связыванию. – MyDaftQuestions

1

Через несколько часов вуду программирования (где я просто попробовать разные вещи в случайном порядке), я нашел решение.

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

Я думаю, что это из-за (см Button сеттер в коде OP)

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

. Каким объектом был «Контент» «собственность принадлежит? – Murven

+0

@Murven, я сделал обновление, это может (или не может) ответить? – MyDaftQuestions

+1

Да, я видел это поведение раньше, ContentPresenter иногда не наследует свой DataContext до своего VisualTree. Мне еще предстоит выяснить, когда и почему именно это происходит. Я рад, что все получилось! – Murven

Смежные вопросы