Je suis nouveau sur ce forum. J'ai un contrôle utilisateur personnalisé défini en utilisant C# et xaml. Lorsque je dag et déposez ce contrôle dans la fenêtre WPF cela fonctionne. Même je peux éditer des étiquettes de code xaml et insérer mon contrôle. Mais quand j'utilise mon contrôle dans le code C#, ça ne marche pas.Pourquoi je ne peux pas utiliser mon contrôle WPF personnalisé dans le code C#
ici est ma définition de contrôle de XAML
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:UserControl"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Resource dictionary entries should be defined here. -->
<Style TargetType="{x:Type local:WellImage}">
<Setter Property="Focusable" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:WellImage}">
<Grid Width="Auto" Height="Auto">
<Ellipse Stroke="{Binding Path=WellBorder, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
StrokeThickness="{Binding Path=WellBorderThickness, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
x:Name="Border" Width="Auto" Height="Auto"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Fill="{Binding Path=OuterBackGround, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
<Ellipse StrokeThickness="0" Margin="25,37,25,18" RenderTransformOrigin="0.5,0.5"
Fill="{Binding Path=InnerBackGround, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
et voici ma c définition # contrôle
using System;
using System.Collections.Generic;
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.Shapes;
namespace UserControl
{
public class WellImage : System.Windows.Controls.Button
{
public static readonly DependencyProperty InnerBackGroundProperty = DependencyProperty.Register("InnerBackGround", typeof(RadialGradientBrush), typeof(WellImage));
public static readonly DependencyProperty OuterBackGroundProperty = DependencyProperty.Register("OuterBackGround", typeof(RadialGradientBrush), typeof(WellImage));
public static readonly DependencyProperty WellBorderProperty = DependencyProperty.Register("WellBorder", typeof(SolidColorBrush), typeof(WellImage));
public static readonly DependencyProperty WellBorderThicknessProperty = DependencyProperty.Register("WellBorderThickness", typeof(double), typeof(WellImage));
public WellImage()
{
// Insert code required on object creation below this point.
InnerBackGround = (RadialGradientBrush)this.Resources["WellSelectedInnerCircleBrush"];
OuterBackGround = (RadialGradientBrush)this.Resources["WellSelectedOuterCircleBrush"];
WellBorder = (SolidColorBrush)this.Resources["NormalBackgroundBrush"];
WellBorderThickness =2;
}
static WellImage()
{
//This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
//This style is defined in themes\generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(typeof(WellImage), new FrameworkPropertyMetadata(typeof(WellImage)));
}
public RadialGradientBrush InnerBackGround
{
get { return (RadialGradientBrush)GetValue(InnerBackGroundProperty); }
set { SetValue(InnerBackGroundProperty, value); }
}
public RadialGradientBrush OuterBackGround
{
get { return (RadialGradientBrush)GetValue(OuterBackGroundProperty); }
set { SetValue(OuterBackGroundProperty, value); }
}
public SolidColorBrush WellBorder
{
get { return (SolidColorBrush)GetValue(WellBorderProperty); }
set { SetValue(WellBorderProperty, value); }
}
public double WellBorderThickness
{
get { return (double)GetValue(WellBorderThicknessProperty); }
set { SetValue(WellBorderThicknessProperty, value); }
}
}
}
et voici comment tenté d'accéder à cette controll via C#
WellImage image = new WellImage();
image.Height = 40;
image.Width = 40;
image.Margin = new Thickness(30, 30, 30, 30);
image.VerticalAlignment = VerticalAlignment.Top;
image.HorizontalAlignment = HorizontalAlignment.Left;
image.Content = "WellButton";
grid.Children.Insert(0, image);
grid.Background = Brushes.LightBlue;
grid.Width = 120;
grid.Height = 100;
grid.VerticalAlignment = VerticalAlignment.Top;
grid.HorizontalAlignment = HorizontalAlignment.Left;
gridPartialedMicroPlate.Children.Insert(0, grid);
Pourquoi je n'arrive pas à accéder à mon contrôle?
Quel est le message d'erreur que vous recevez lorsque vous essayez de l'appeler par programme? – Aaronontheweb
Salut Peter, votre code compile-t-il? Je veux dire le code où vous montrez comment vous avez essayé d'y accéder, est-ce que cela compile? IntelliSense trouve-t-il l'objet image après l'avoir créé? Ou voulez-vous dire que si vous utilisez le débogueur, vous ne le voyez jamais construit? – jameschinnock