【问题标题】:Why won't my Template's TextBox bind为什么我的模板的文本框无法绑定
【发布时间】:2014-12-18 21:22:58
【问题描述】:

我正在尝试做一些绑定。在大多数情况下,我的 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 路径错误:在“对象”“ColumnPrimitiveInfo”(HashCode=39288004)上找不到“ShortDate”属性。绑定表达式:路径=短日期; DataItem='ColumnPrimitiveInfo' (HashCode=39288004);目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)

如何让绑定生效

  <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 的源代码,如果你下载应用程序,点击图表 -> 样式 -> 列系列你可以看到我的代码非常相似。有作品,但我的没有,我不明白为什么。请注意,图表本身会显示,只是文本框没有显示。但是,如果我不使用绑定,而只使用Text="SomeWords",那么它可以正常工作

【问题讨论】:

  • 您的 GraphData 属性在哪里?在您的 ViewModel 中还是 xceed 属性?你能给我们一个链接到你使用的组件的文档吗?在xceed网站上找不到。 doc.xceedsoft.com/products/XceedChart
  • @nkoniishvt 您可以从xceed.com/WPF_Toolkit_Demo.html 下载他们的演示(显示源代码)-加载后,在图表下展开样式选项并单击列系列。从这里,您可以在右侧看到 XAML 代码(有效)。我几乎复制和粘贴(它有效)然后改变了一些东西并打破了它。我看不到我弄坏了什么
  • 请发布您的 ViewModel 类并发布完整的 xaml
  • @nkoniishvt,应要求提供完整代码
  • 你在xaml后端代码中设置了DataContext吗?

标签: c# wpf xaml mvvm


【解决方案1】:

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

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

我认为这是因为(参见 OP 代码中的按钮设置器)

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

【讨论】:

  • “内容”属性属于什么对象?
  • 是的,我以前见过这种行为,ContentPresenter 有时不会沿其 VisualTree 继承其 DataContext。我还没有找到发生这种情况的确切时间或原因。我很高兴它成功了!
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:

    通过查看此处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>
    

    【讨论】:

    • 遗憾的是,我认为它需要这个额外的层,因为图形不是可视化树的一部分,因此需要添加这个才能使绑定工作
    猜你喜欢
    • 2013-03-18
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2020-08-10
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    相关资源
    最近更新 更多