自定义控件关于属性值作为内容显示?
wpf吧
全部回复
仅看楼主
level 1
随便写了个用户控件类:
class MyControl :Control {
#region 控件显示内容
public string ShowText {
get { return (string)GetValue(TEXTProperty); }
set { SetValue(TEXTProperty, value); }
}
public static readonly DependencyProperty TEXTProperty =
DependencyProperty.Register("ShowText",
typeof(string),
typeof(MyControl),
new FrameworkPropertyMetadata(""));
#endregion
}
我在Grid中定义了一个长宽都为500的Canvas画布:
<Grid>
<Canvas x:Name="mainCanvas" Width="500" Height="500" Background="Bisque">
</Canvas>
</Grid>
然后在加载事件中写:
private void UserControl_Loaded(object sender, RoutedEventArgs e) {
double x = 250;
double y = 250;
MyControl col = new MyControl();
col.ShowText = "测试";
col.Width = 50;
col.Height = 30;
Canvas.SetLeft(col, x);
Canvas.SetTop(col, y);
col.Visibility = Visibility.Visible;
this.mainCanvas.Children.Add(col);
double x1 = 200;
double y1 = 200;
Label lb = new Label();
lb.Content = "测试1";
lb.Width = 50;
lb.Height = 30;
Canvas.SetLeft(lb, x1);
Canvas.SetTop(lb, y1);
//lb.Visibility = Visibility.Visible;
this.mainCanvas.Children.Add(lb);
}
但最终结果只会显示Label控件的,我自定义的控件却不显示,这是为什么呢;
总是觉得缺了什么东西,怎么让我这个控件也显示并且控件会把这个属性的值显示出来呢,像Label控件那样;
2021年01月06日 05点01分 1
level 2
因为你的空间没有任何可视化元素
2021年01月14日 03点01分 3
1