嗨,我正在使用 userControl,它工作正常,但是当我第二次尝试使用它时,它会引发下一个异常
System.Windows.dll 中发生“System.ArgumentException”类型的未处理异常附加信息:参数不正确。
这是我的代码
XAML
<Grid x:Name="LayoutRoot">
<TextBlock x:Name="tb1" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=me}" TextWrapping="Wrap" />
<TextBlock x:Name="tb2" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
<TextBlock x:Name="tb3" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
<TextBlock x:Name="tb4" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
<TextBlock x:Name="tb5" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
<TextBlock x:Name="tb6" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
<TextBlock x:Name="tb7" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
<TextBlock x:Name="tb8" FontWeight="ExtraBold" Foreground="White" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
</Grid>
C#
public partial class DropShadowTextBlock : UserControl
{
public DropShadowTextBlock()
{
InitializeComponent();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public SolidColorBrush Foreground
{
get { return (SolidColorBrush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty,value);}
}
public static void ForegroundPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
DropShadowTextBlock control = sender as DropShadowTextBlock;
if (control != null)
{
control.tb8.Foreground = e.NewValue as SolidColorBrush;
}
}
public static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as DropShadowTextBlock;
if (control.tb1 != null)
control.tb1.Text = e.NewValue.ToString();
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DropShadowTextBlock), new PropertyMetadata(new PropertyChangedCallback(TextPropertyChanged)));
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register("Foreground", typeof(SolidColorBrush), typeof(DropShadowTextBlock), new PropertyMetadata(new PropertyChangedCallback(ForegroundPropertyChanged)));
}
}
最后这是我使用 userControl 的方式
public void dibujarTexto(String text)
{
DropShadowTextBlock textblockTop = new DropShadowTextBlock();
textblockTop.Text = text;
textblockTop.Foreground = new SolidColorBrush(Colors.White);
textblockTop.Name = Guid.NewGuid().ToString();
migrid.Children.Add(textblockTop); //this is a grid control
}
我一直在分享,我发现控件名称必须是唯一的,所以我使用 Guid 类来获取随机名称但不起作用
请如果有人可以帮助我。
谢谢最好的问候。