如果我想创建由两个控件组成的 Bindable 用户控件,我想询问正确的方法。我不确定我在做什么——我是否做对了,因为我遇到了一些问题。
这是我正在尝试做的事情:
让我们将此控件称为 ucFlagControl 。创建新的自定义用户控件...其目的是在 Bool 类型的变量中显示逻辑(真/假)值的颜色解释。
我以前做的是使用Rectangle, 和 Bind FillPropertyto boolean value usingConverter
我为使它起作用所做的工作是,我制作了一个 usercontrol ,然后将矩形和标签放入其中,然后添加了以下代码:
public partial class ucStatusFlag : UserControl
{
public ucStatusFlag()
{
InitializeComponent();
}
public string LabelContent
{
get { return (string)GetValue(LabelContentProperty); }
set
{
SetValue(LabelContentProperty, value);
OnPropertyChanged("LabelContent");
}
}
///in case that I use integer or array
public int BitIndex
{
get { return (int)GetValue(BitIndexProperty); }
set
{
SetValue(BitIndexProperty, value);
OnPropertyChanged("BitIndex");
}
}
public string BindingSource
{
get { return (string)GetValue(BindingSourceProperty); }
set
{
SetValue(BindingSourceProperty, value);
OnPropertyChanged("BindingSource");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
/// <summary>
/// Identified the Label dependency property
/// </summary>
public static readonly DependencyProperty LabelContentProperty =
DependencyProperty.Register("LabelContent", typeof(string), typeof(ucStatusFlag), new PropertyMetadata("LabelContent"));
public static readonly DependencyProperty BitIndexProperty =
DependencyProperty.Register("BitIndex", typeof(int), typeof(ucStatusFlag), new PropertyMetadata(0));
public static readonly DependencyProperty BindingSourceProperty =
DependencyProperty.Register("(BindingSource", typeof(string), typeof(ucStatusFlag), new PropertyMetadata(""));
private void StatusFlag_Loaded(object sender, RoutedEventArgs e)
{
if (BindingSource.Length > 0)
{
Binding bind = new Binding();
string s = LabelContent;
int i = BitIndex;
bind.Converter = new StatusToColor();
bind.Path = new PropertyPath(BindingSource);
bind.ConverterParameter = BitIndex.ToString();
bind.Mode = BindingMode.OneWay;
bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
recStatusBit.SetBinding(Rectangle.FillProperty, bind);
}
}
private class StatusToColor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte bDataWordIdx;
byte bDataBitIdx;
Byte.TryParse((string)parameter, out bDataBitIdx);
if (Object.ReferenceEquals(typeof(UInt16[]), value.GetType()))
{
UInt16[] uiaData = (UInt16[])value;
bDataWordIdx = (byte)uiaData[0];
if ((uiaData[bDataBitIdx / 16] >> (bDataBitIdx % 16) & 0x1) == 1)
{
return Brushes.Green;
}
else
{
return Brushes.Red;
}
}
else if (Object.ReferenceEquals(typeof(UInt16), value.GetType()))
{
UInt16 uiaData = (UInt16)value;
if (((uiaData >> bDataBitIdx) & 0x1) == 1)
{
return Brushes.Green;
}
else
{
return Brushes.Red;
}
}
return 0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return 0;
}
}
}
比我意识到我可以轻松绑定内容而且我不必创建public static readonly DependencyProperty LabelContentProperty
但只是财产
public new string Content
{
get { return (string)label.Content; }
set
{
SetValue(label.Content, value);
OnPropertyChanged("Content");
}
}
这会覆盖原始内容,因此我能够在上层绑定和/或分配标签的文本 - 例如在放置此用户控件的 MainWindow.xaml
第一个问题是在这种情况下是否可以,或者是否有一些我不知道的背景,我什至应该以不同的方式做这样的小控件 - 我想制作 dll。从它加载到工具箱 - 我测试了它的工作原理。而不是在例如 stack panel 中使用它。
第二个问题是我的“矩形”Fill"属性有问题。我无法像绑定内容一样绑定该属性。我知道矩形是从 Shape 类派生的,所以我不确定它是否与此有关。
如果我能够进行与中相同的内部绑定或连接
Content
我可以删除转换器,然后将其绑定到例如 MainWindow.xaml 文件中(使用转换器和转换器参数)
但FillProperty对我不起作用,所以我不确定我的观点。
谢谢你的建议
编辑:
好吧,我很抱歉,但我没有在下面的评论中听到你想说的所有内容。你能解释一下吗?我知道上面的代码不是正确的方法......?或者你能发表任何关于它的文章吗?我的实际代码是这样的:在用户控件中...我从后面的代码中删除了所有代码...
' <Label x:Name="lStatusBit" Grid.Column="1" Padding="0" VerticalContentAlignment="Center" Margin="2,1,17,2" />
<Rectangle x:Name="recStatusBit" Margin="0,3,1,7" />'
Content 属性有效,我看不到 Rectangle 和 rectangle fill 属性...其他问题是,如果我在放置我的 uc 的 XAML 中填写 Content 属性,则 Rectangle 消失。