我想询问正确的方法,是否要创建由两个控件组成的可绑定用户控件。我不确定自己在做什么-是否正确执行操作,因为遇到了一些问题。
这里就是我想要做的事:
让我们将此控件称为ucFlagControl。建立新的,自定义用户控件...其目的是显示变量(布尔类型)中逻辑(真/假)值的颜色解释。
以前我曾经使用Rectangle
,并使用FillProperty
将bool
绑定到Converter
ean值
我做了什么让它的工作原理是,我做了一个用户控件,并把矩形和里面的标签比我添加此代码:
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。从它加载到工具箱-我测试了它的工作原理。而不是在堆栈面板中使用它。
第二个问题是我对矩形的[Fill"
属性有问题。我无法像绑定内容一样绑定该属性。我知道矩形是从Shape类派生的,所以我不确定是否与此有关。
如果我能够做内的结合或连接相同
Content
我可以删除转换器,然后将其绑定到例如MainWindow.xaml文件(使用converter和converter参数)
但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" />'
内容属性有效,我看不到Rectangle和矩形fill属性...另一个问题是,如果我在放置uc的XAML中填充Content属性,则Rectangle消失。
嗯,可能我感到困惑,因为我首先尝试的是搜索自己的用户控件等,并且出现了几个不同的答案。
我可以简单地做到这就是:
在自定义用户控制:/代码后面:xaml.cs文件
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string UserControlText
{
get { return (String)GetValue(UserControlTextProperty); }
set { SetValue(UserControlTextProperty, value); }
}
public Brushes UserControlFlagColor
{
get { return (Brushes)GetValue(UserControlBackgroundBrushProperty); }
set
{
SetValue(UserControlBackgroundBrushProperty, value);
}
}
public static readonly DependencyProperty UserControlTextProperty =
DependencyProperty.Register("UserControlTextProperty", typeof(string), typeof(UserControl1), new PropertyMetadata(0));
public static readonly DependencyProperty UserControlBackgroundBrushProperty =
DependencyProperty.Register("UserControlBackgroundBrushProperty", typeof(Brushes), typeof(UserControl1), new PropertyMetadata(Brushes.Gray));
}
然后例如在新项目
右键单击工具箱 - >选择物品 - > CARD WPF组件 - >浏览
只需选择您的DLL并拖放或写入xaml,例如:
所以是我的错,我试图做的太复杂的方式。感谢您的回答或建议。
<Grid>
<CustomToolTip:UserControl1 UserControlTextProperty="Aqua" UserControlText="abcdef" HorizontalAlignment="Left" Height="100" Margin="104,84,0,0" VerticalAlignment="Top" Width="202"/>
</Grid>