自定义控件中的依赖属性不更新

时间:2013-09-30 15:21:18

标签: c# wpf custom-controls

我正在尝试创建一个自定义控件,它基本上是位于数据网格列标题上方的TextBox,并在datagrid列中显示总数。控件库由两个自定义控件组成:DataGridColumnTotal包含文本框,该文本框还包含对DataGrid和ItemsSource的引用。另一个叫做DataGridHeaderTotalControl,它基本上是一个以DataGridColumnHeader为目标的样式。此样式将DataGridColumnTotal控件放在现有DatagridColumnHeader之上,并将DataGridColumnTotal控件中的DataGrid和DataGridItemsSource字段绑定到Datagrid和ItemsSource字段。

我目前正在尝试让TextBox显示是否设置了IsTotalVisible依赖项属性,但是当我尝试在MainWindow.xaml中设置它时它不会更新。我没有任何显示总价值的代码,因为我还没有那么远。

当尝试在dataGridText列中设置ctl:DataGridColumnTotal.IsTotalVisible =“True”时,MainWindow.xaml中出现问题。 IsTotalVisible属性未设置为true。但是当我将依赖项属性的默认值设置为true时,将显示总文本框。

DataGridColumnTotal.cs

////////////////////////////////////////////////////////////////////
//DataGridColumnTotal.cs
////////////////////////////////////////////////////////////////////
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;

namespace RiskControlLibrary
{
    public class DataGridColumnTotal : Control
    {
        static DataGridColumnTotal()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridColumnTotal), new FrameworkPropertyMetadata(typeof(DataGridColumnTotal)));
        }

        public static bool GetIsTotalVisible(
            DependencyObject target)
        {
            return (bool)target.GetValue(IsTotalVisibleProperty);
        }

        public static void SetIsTotalVisible(
            DependencyObject target, bool value)
        {
            target.SetValue(IsTotalVisibleProperty, value);
        }

        public static DependencyProperty IsTotalVisibleProperty =
        DependencyProperty.RegisterAttached("IsTotalVisible", typeof(bool), typeof(DataGridColumnTotal), 
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));

    }

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("Not Implemented.");
        }
    }
}

DataGridHeaderTotalControl.cs

////////////////////////////////////////////////////////////////////
//DataGridHeaderTotalControl.cs
////////////////////////////////////////////////////////////////////
using System.Windows;
using System.Windows.Controls;

namespace RiskControlLibrary
{
    public class DataGridHeaderTotalControl : Control
    {
        static DataGridHeaderTotalControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridHeaderTotalControl), new FrameworkPropertyMetadata(typeof(DataGridHeaderTotalControl)));
        }
    }
}

Generic.xaml

////////////////////////////////////////////////////////////////////
//Generic.xaml
////////////////////////////////////////////////////////////////////
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:RiskControlLibrary"
    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:s="clr-namespace:System;assembly=mscorlib">

    <Style TargetType="{x:Type local:DataGridColumnTotal}">
        <Style.Resources>
            <local:BoolToVisibilityConverter x:Key="booleanToVisibilityConverter" />
        </Style.Resources>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DataGridColumnTotal}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBox
                            x:Name="PART_TextBoxTotal"
                            IsReadOnly="True"
                            VerticalAlignment="Top"
                            VerticalContentAlignment="Center"
                            Text="{Binding Total,
                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:DataGridColumnTotal}}}"
                            Visibility="{Binding IsTotalVisible, RelativeSource={RelativeSource TemplatedParent},
                            Converter={StaticResource booleanToVisibilityConverter}}"
                         >
                        </TextBox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type DataGridColumnHeader}"
           x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:DataGridHeaderTotalControl},
           ResourceId=DataGridHeaderTotalControlStyle}">
        <Setter Property="Template">

            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">

                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <local:DataGridColumnTotal Margin="1" Grid.Column="0" Grid.Row="0"/>

                            <theme:DataGridHeaderBorder Grid.Column="0" Grid.Row="1" SortDirection="{TemplateBinding SortDirection}"
                                     IsHovered="{TemplateBinding IsMouseOver}"
                                     IsPressed="{TemplateBinding IsPressed}"
                                     IsClickable="{TemplateBinding CanUserSort}"
                                     Background="{TemplateBinding Background}"
                                     BorderBrush="{TemplateBinding BorderBrush}"
                                     BorderThickness="{TemplateBinding BorderThickness}"
                                     Padding ="{TemplateBinding Padding}"
                                     SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
                                     SeparatorBrush="{TemplateBinding SeparatorBrush}">

                            <TextBlock Grid.Column="0" Grid.Row="1"  Text="{TemplateBinding  Content}" 
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       TextWrapping="Wrap"></TextBlock>

                        </theme:DataGridHeaderBorder>

                    </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MainWindow.xaml

////////////////////////////////////////////////////////////////////
//MainWindow.xaml
////////////////////////////////////////////////////////////////////
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:WpfApplication1"
        xmlns:ctl="clr-namespace:RiskControlLibrary;assembly=RiskControlLibrary"
        Title="MainWindow" Height="341" Width="452"
        Loaded="Window_Loaded">
    <Grid>
        <DataGrid 
            Name="m_dgOrder"
            ItemsSource="{Binding ListPos}"
            GridLinesVisibility="None"
            AutoGenerateColumns="False"
            IsReadOnly="True"
            Background="White"
            CanUserResizeRows="False"
            HeadersVisibility="Column"
            RowHeaderWidth="0"
            ColumnHeaderStyle="{StaticResource {ComponentResourceKey 
            TypeInTargetAssembly={x:Type ctl:DataGridHeaderTotalControl}, 
            ResourceId=DataGridHeaderTotalControlStyle}}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Symbol" Binding="{Binding Path=Symbol}"/>
                <DataGridTextColumn Header="Pos" Binding="{Binding Path=Pos}" ctl:DataGridColumnTotal.IsTotalVisible="True"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

我正在将IsTotalVisible属性注册到错误的类。它应该是DataGridTextColumn。

而不是DataGridColumnTotal