为什么没有XAML控件的UserControl不能继承属性

本文关键字:UserControl 不能 继承 属性 控件 XAML 为什么 | 更新日期: 2023-09-27 18:17:42

我在XAML的WPF中完成了一个工作用户控件。现在我已经删除了XAML并使用代码创建了用户控件,因为我需要继承这个控件。之前我在使用控件的代码上有这样的代码:

<mc:MyControl Foreground="White">

MyControl控件中的所有控件都使用了这个前景设置。现在我已经使用c#创建了控件,这种情况不再发生了。

有人知道为什么和如何解决这个问题?

提前感谢。

编辑:

这是XAML:
<UserControl x:Class="Utils.Wpf.Chart.Chart"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         SizeChanged="UserControl_SizeChanged">
<Grid>
    <Border Margin="5" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Canvas Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2"  x:Name="DrawArea" Canvas.Background="Transparent" MouseEnter="DrawArea_MouseEnter" MouseLeave="DrawArea_MouseLeave" MouseMove="DrawArea_MouseMove" Cursor="None">
                <Line x:Name="Border1" X1="0" X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" Y1="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" StrokeThickness="1" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
                <Line x:Name="Border2" Y1="0" Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" X1="0" X2="0" StrokeThickness="1" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
                <TextBlock x:Name="CrossValue" Text="[-,-]" Foreground="#EBDE11" Visibility="Hidden"/>
            </Canvas>
            <TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MaxYCaption, TargetNullValue=MaxY}" Margin="3,0,6,0"/>
            <TextBlock Grid.Column="0" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MinYCaption, TargetNullValue=MinY}" Margin="3,0,6,0"/>
            <TextBlock Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MinX, TargetNullValue=MinX}" Margin="3,3,0,0"/>
            <TextBlock Grid.Column="2" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Top" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MaxX, TargetNullValue=MaxX}" Margin="0,3,3,0"/>
        </Grid>
    </Border>
</Grid>
</UserControl>

c#:

        _outerGrid = new Grid();
        _border = new Border();
        _innerGrid = new Grid();
        _canvas = new Canvas();
        _line1 = new Line();
        _line2 = new Line();
        _cursorText = new TextBlock();
        _maxXText = new TextBlock();
        _maxYText = new TextBlock();
        _minXText = new TextBlock();
        _minYText = new TextBlock();
        this.Content = _outerGrid;
        _border.Margin = new Thickness(5);
        _border.HorizontalAlignment = HorizontalAlignment.Stretch;
        _border.VerticalAlignment = VerticalAlignment.Stretch;
        _outerGrid.Children.Add(_border);
        _column1 = new ColumnDefinition();
        _column1.Width = new GridLength(0, GridUnitType.Auto);
        _column2 = new ColumnDefinition();
        _column2.Width = new GridLength(1, GridUnitType.Star);
        _column3 = new ColumnDefinition();
        _column3.Width = new GridLength(1, GridUnitType.Star);
        _row1 = new RowDefinition();
        _row1.Height = new GridLength(1, GridUnitType.Star);
        _row2 = new RowDefinition();
        _row2.Height = new GridLength(1, GridUnitType.Star);
        _row3 = new RowDefinition();
        _row3.Height = new GridLength(0, GridUnitType.Auto);
        _innerGrid.ColumnDefinitions.Add(_column1);
        _innerGrid.ColumnDefinitions.Add(_column2);
        _innerGrid.ColumnDefinitions.Add(_column3);
        _innerGrid.RowDefinitions.Add(_row1);
        _innerGrid.RowDefinitions.Add(_row2);
        _innerGrid.RowDefinitions.Add(_row3);
        _border.Child = _innerGrid;
        _canvas.Background = Brushes.Transparent;
        _canvas.Cursor = Cursors.None;
        _canvas.MouseEnter += new MouseEventHandler(_canvas_MouseEnter);
        _canvas.MouseMove += new MouseEventHandler(_canvas_MouseMove);
        _canvas.MouseLeave += new MouseEventHandler(_canvas_MouseLeave);
        Grid.SetColumn(_canvas,1);
        Grid.SetColumnSpan(_canvas,2);
        Grid.SetRowSpan(_canvas,2);
        _innerGrid.Children.Add(_canvas);
        _line1.X1 = 0;
        _line1.StrokeThickness = 1;
        Binding x2Binding = new Binding("ActualWidth");
        x2Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) {AncestorType = _canvas.GetType()};
        _line1.SetBinding(Line.X2Property, x2Binding);
        Binding yBinding = new Binding("ActualHeight");
        yBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = _canvas.GetType() };
        _line1.SetBinding(Line.Y1Property, yBinding);
        _line1.SetBinding(Line.Y2Property, yBinding);
        _line2.Y1 = 0;
        _line2.X1 = 0;
        _line2.X2 = 0;
        _line2.StrokeThickness = 1;
        Binding y2Binding = new Binding("ActualHeight");
        y2Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = _canvas.GetType() };
        _line2.SetBinding(Line.Y2Property, x2Binding);
        Binding strokeBinding = new Binding("BorderBrush");
        strokeBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _line2.SetBinding(Shape.StrokeProperty, strokeBinding);
        _cursorText.Text = "[-,-]";
        _cursorText.Foreground = new SolidColorBrush(Color.FromRgb(0xeb, 0xde, 11));
        _cursorText.Visibility = Visibility.Hidden;
        _canvas.Children.Add(_line1);
        _canvas.Children.Add(_line2);
        _canvas.Children.Add(_cursorText);
        _maxYText.VerticalAlignment = VerticalAlignment.Top;
        _maxYText.HorizontalAlignment = HorizontalAlignment.Right;
        _maxYText.Margin = new Thickness(3, 0, 6, 0);
        Binding maxYBinding = new Binding("MaxYCaption");
        maxYBinding.TargetNullValue = "MaxY";
        maxYBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _maxYText.SetBinding(TextBlock.TextProperty, maxYBinding);
        _minYText.VerticalAlignment = VerticalAlignment.Bottom;
        _minYText.HorizontalAlignment = HorizontalAlignment.Right;
        _minYText.Margin = new Thickness(3, 0, 6, 0);
        Binding minYBinding = new Binding("MinYCaption");
        minYBinding.TargetNullValue = "MinY";
        minYBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _minYText.SetBinding(TextBlock.TextProperty, minYBinding);
        _maxXText.VerticalAlignment = VerticalAlignment.Top;
        _maxXText.HorizontalAlignment = HorizontalAlignment.Right;
        _maxXText.Margin = new Thickness(0, 3, 3, 0);
        Binding maxXBinding = new Binding("MaxX");
        maxXBinding.TargetNullValue = "MaxX";
        maxXBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _maxXText.SetBinding(TextBlock.TextProperty, maxXBinding);
        _minXText.VerticalAlignment = VerticalAlignment.Top;
        _minXText.HorizontalAlignment = HorizontalAlignment.Left;
        _minXText.Margin = new Thickness(3, 3, 0, 0);
        Binding minXBinding = new Binding("MinX");
        minXBinding.TargetNullValue = "MinX";
        minXBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
        _minXText.SetBinding(TextBlock.TextProperty, minXBinding);
        _innerGrid.Children.Add(_maxYText);
        _innerGrid.Children.Add(_maxXText);
        _innerGrid.Children.Add(_minYText);
        _innerGrid.Children.Add(_minXText);
        Grid.SetColumn(_maxXText, 2);
        Grid.SetColumn(_minXText, 1);
        Grid.SetRow(_minYText, 1);
        Grid.SetRow(_minXText, 2);
        Grid.SetRow(_maxXText, 2);
        this.SizeChanged += new System.Windows.SizeChangedEventHandler(Chart2_SizeChanged);

为什么没有XAML控件的UserControl不能继承属性

@SoMoS -我认为你所说的是一个CustomControl,而不是UserControl。如果是,您是否将控件模板应用于继承的控件?

你可以在这里阅读自定义控件和用户控件之间的区别-

http://wangmo.wordpress.com/2007/09/28/user-controls-vs-custom-controls/

http://www.wpftutorial.net/CustomVsUserControl.html

如果您将控件模板应用于自定义控件,则可以像使用XAML管理用户控件一样管理其外观。