当前位置:编程学习 > C#/ASP.NET >>

关于WPF自定义空间库的问题

最近在学习WPF,并尝试写了一个自定义控件,但是,遇到两个问题:
将自定义控件拖放到窗体后,
1、如果自定义样式表中是动态绑定样式,则控件在窗体上看不到,除非是静态绑定样式。
2、更改窗体中控件的属性,发现控件没有发生变化

代码如下:我中间会用红字说明

1、自定义控件cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfCustomControlLibrary1
{
    public class CustomControl : Control
    {
        public static readonly DependencyProperty ShowBorderProperty;

        /// <summary>
        /// 获取或设置显示边框
        /// </summary>
        public bool ShowBorder
        {
            get { return (bool)GetValue(ShowBorderProperty); }
            set { SetValue(ShowBorderProperty, value); }
        }


        static CustomControl()
        {
            ShowBorderProperty = DependencyProperty.Register("ShowBorder", typeof(bool), typeof(CustomControl), new PropertyMetadata(false));
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
        }
    }
}


2、自定义控件样式文件

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary1">
    <ControlTemplate x:Key="BorderTemplate" TargetType="{x:Type ContentControl}">
        <Grid>
            <Grid.Resources>
                <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
                <local:BoolToVisibilityInverseConverter x:Key="BoolToVisibilityInverseConverter"/>
            </Grid.Resources>
            <Border Background="#FF7496C2" BorderThickness="0" CornerRadius="3" Padding="1" Visibility="{Binding Path=ShowBorder, Converter={StaticResource BoolToVisibilityConverter}}">
                <Grid>
                    <Border Margin="0" BorderThickness="0" CornerRadius="2" Background="#FFEAF2FC" />
                    <Border Margin="1" CornerRadius="1" Background="#FFCEE1F8">
                    </Border>
                </Grid>
            </Border>
            <Grid Background="#FFCEE1F8" Visibility="{Binding Path=ShowBorder, Converter={StaticResource BoolToVisibilityInverseConverter}}" />
            <ContentPresenter Margin="11" />
        </Grid>
    </ControlTemplate>
    <ControlTemplate x:Key="ContentTemplate" TargetType="{x:Type local:CustomControl}">
        <Grid SnapsToDevicePixels="True">
            <ContentControl x:Name="PART_Border" HorizontalContentAlignment="Left" Template="{StaticResource(这里不能用动态资源?) BorderTemplate}" VerticalContentAlignment="Stretch" Focusable="False">
                <Grid>
                    <ContentControl Grid.Row="2" Focusable="False" />
                </Grid>
            </ContentControl>
        </Grid>
    </ControlTemplate>
    <Style TargetType="{x:Type local:CustomControl}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template" Value="{StaticResource(这里不能用动态资源?) ContentTemplate}"/>
    </Style>
</ResourceDictionary>


3、样式中用到的两个cs(发现好像不起作用,程序根本没有调用这里,不能断点)

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WpfCustomControlLibrary1
{
    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (((bool)value) ? Visibility.Visible : Visibility.Collapsed);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (((Visibility)value) == Visibility.Visible);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows;

namespace WpfCustomControlLibrary1
{
    public class BoolToVisibilityInverseConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (((bool)value) ? Visibility.Collapsed : Visibility.Visible);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (((Visibility)value) == Visibility.Collapsed);
        }
    }
}


4、窗体代码

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:my="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1">
    <Grid>
        <my:CustomControl HorizontalAlignment="Left" Margin="83,57,0,0" Name="customControl1" VerticalAlignment="Top" Height="193" Width="231" ShowBorder="True"(就是这里,在设计状态和运行状态,都没有变化) />
    </Grid>
</Window>
--------------------编程问答--------------------

//设置边框大小
        public static new readonly DependencyProperty BorderThicknessProperty;
        public new Thickness BorderThickness
        {
            get { return (Thickness)GetValue(BorderThicknessProperty); }
            set { SetValue(BorderThicknessProperty, value); }
        }
        private static void BorderThicknessChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TMarquee tMarquee = (TMarquee)sender;
            tMarquee.border1.BorderThickness = tMarquee.BorderThickness;
        }

static TMarquee()
        {
            BorderThicknessProperty = DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(TMarquee), new FrameworkPropertyMetadata(new Thickness(3), BorderThicknessChange),
                ShareClass.ValueTypeCheck<Thickness>);
        }


没有定义DependencyPropertyChangedCallback --------------------编程问答-------------------- tMarquee.border1.BorderThickness = tMarquee.BorderThickness;

????

border1是样式表中的一个element的名字吗?为什么BoolToVisibilityConverter没有被调用呢? --------------------编程问答-------------------- 2、自定义控件样式,上面发的有问题,重新发一遍

这里有转换方法调用(11、18行),和两个设置Template时候的绑定(24、33行)
为什么实际运行时候没有调用转换方法?
为什么设置Template绑定的时候用动态绑定后,拖到界面上的控件样式没有起作用呢?
要自定义一个控件,然后生成dll供别人使用,在别人引用后,拖到窗体上就是带有默认样式的,应该怎么做呢?谁给个例子?


1<ResourceDictionary
2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4    xmlns:local="clr-namespace:WpfCustomControlLibrary1">
5    <ControlTemplate x:Key="BorderTemplate" TargetType="{x:Type ContentControl}">
6        <Grid>
7            <Grid.Resources>
8                <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
9                <local:BoolToVisibilityInverseConverter x:Key="BoolToVisibilityInverseConverter"/>
10            </Grid.Resources>
11            <Border Background="#FF7496C2" BorderThickness="0" CornerRadius="3" Padding="1" Visibility="{Binding Path=ShowBorder, Converter={StaticResource BoolToVisibilityConverter}}">
12                <Grid>
13                    <Border Margin="0" BorderThickness="0" CornerRadius="2" Background="#FFEAF2FC" />
14                    <Border Margin="1" CornerRadius="1" Background="#FFCEE1F8">
15                    </Border>
16                </Grid>
17            </Border>
18            <Grid Background="#FFCEE1F8" Visibility="{Binding Path=ShowBorder, Converter={StaticResource BoolToVisibilityInverseConverter}}" />
19            <ContentPresenter Margin="11" />
20        </Grid>
21    </ControlTemplate>
22    <ControlTemplate x:Key="ContentTemplate" TargetType="{x:Type local:CustomControl}">
23        <Grid SnapsToDevicePixels="True">
24            <ContentControl x:Name="PART_Border" HorizontalContentAlignment="Left" Template="{StaticResource BorderTemplate}" VerticalContentAlignment="Stretch" Focusable="False">
25                <Grid>
26                    <ContentControl Grid.Row="2" Focusable="False" />
27                </Grid>
28            </ContentControl>
29        </Grid>
30    </ControlTemplate>
31    <Style TargetType="{x:Type local:CustomControl}">
32        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
33        <Setter Property="Template" Value="{StaticResource ContentTemplate}"/>
34    </Style>
35</ResourceDictionary>
--------------------编程问答-------------------- 除
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,