这是该网站的长期访问者,但这将是我的第一篇文章。希望我能在这里得到一些帮助。环顾四周,到目前为止没有运气,或者我误读了我正在看的东西。这是我想要克服的情况。
我正在尝试将(主窗口)TextBlock文本绑定到主窗口上用户控件内的自定义控件的DependencyProperty。
像这样打破。 类库项目:BindTest 自定义控制:
class DocUploadForm(autocomplete_light.ModelForm):
class Meta:
model = Document
# widgets = {'tags' : autocomplete_light.MultipleChoiceWidget('TagAutocomplete')}
autocomplete_fields = ('tags',)
exclude = ['organization','private_user']
现在我有一个用户控件将使用此自定义控件(PickerPlugin.xaml)后面的代码中没有任何内容。这里没什么特别的。
namespace BindTest.Controls
{
using System.Windows;
using System.Windows.Controls;
public class Picker : Control
{
static Picker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Picker), new FrameworkPropertyMetadata(typeof(Picker)));
}
/// <summary>
/// Hue Property
/// </summary>
public double Hue
{
get { return (double)GetValue(HueProperty); }
set { SetValue(HueProperty, value); }
}
public static readonly DependencyProperty HueProperty = DependencyProperty.Register("Hue", typeof(double), typeof(Picker),
new FrameworkPropertyMetadata(0.0,
new PropertyChangedCallback(UpdateColorHSB),
new CoerceValueCallback(HueCoerce)));
public static object HueCoerce(DependencyObject d, object Hue)
{
double v = (double)Hue;
if (v < 0) return 0.0;
if (v > 1) return 1.0;
return v;
}
/// <summary>
/// Shared property changed callback to update the Color property
/// </summary>
public static void UpdateColorHSB(object o, DependencyPropertyChangedEventArgs e)
{
Picker c = (Picker)o;
// Do work
}
}
}
这就是控制。我想现在在我的主窗口上使用这个控件但是我希望能够从这个元素访问Hue属性(cntrls:Picker x:Name =&#34; C&#34;)就像我在控件中这里一样。
我在主窗口中的所有内容如下:
<UserControl
x:Class="BindTest.PickerPlugin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BindTest"
xmlns:cntrls="clr-namespace:BindTest.Controls"
FontSize="7.75pt" Foreground="#DDD" FontFamily="Verdana"
Width="200" Height="200">
<Grid>
<StackPanel Margin="1">
<TextBlock Margin="0,2,0,0" HorizontalAlignment="Center">Model</TextBlock>
<cntrls:Picker x:Name="C" />
<TextBlock Text="{Binding ElementName=C, Path=Hue}" />
</StackPanel>
</Grid>
</UserControl>
我无法弄清楚如何从该用户控件访问该Hue属性。我希望有一些方法可以从用户控件中的元素访问该Hue属性?希望这是有道理的,有人可以提供帮助!
谢谢!
答案 0 :(得分:0)
嗯,我确信我只是从那篇文章中复制一些内容,但也许对于你来说,更容易理解其他样本中的这些内容。
我创建了新的解决方案WpfTest和两个项目:WpfTest和WpfControlLibrary1。
WpfTest的MainWindow.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest"
xmlns:WpfControlLibrary1="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1" x:Class="WpfTest.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="{Binding Some, ElementName=UserControl1}" Margin="239,0,0,188" />
<WpfControlLibrary1:UserControl1 Name="UserControl1" HorizontalAlignment="Left" Margin="60,54,0,0" VerticalAlignment="Top"/>
<Button Click="ButtonBase_OnClick" Margin="0,224,164,0" />
</Grid>
WpfControlLibrary1.UserControl1.xaml
<UserControl x:Class="WpfControlLibrary1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfControlLibrary1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<local:SomeControl Width="100" Height="100" Content="{Binding Some, RelativeSource={RelativeSource Self}}" />
</Grid>
如您所见,我还创建了SomeControl:
public class SomeControl : ContentControl
{
public static readonly DependencyProperty SomeProperty = DependencyProperty.Register("Some", typeof (int),
typeof (SomeControl), new FrameworkPropertyMetadata(10) {Inherits = true});
public int Some
{
get { return (int) GetValue(SomeProperty); }
set { SetValue(SomeProperty, value); }
}
}
我们这里有SomeProperty。它的类型是int,它有包装器,标记为可继承的元数据。 如果我们想在MainWindow中使用它,我们必须在UserControl级别创建一些东西,让我们看看我们有什么:
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty SomeProperty = SomeControl.SomeProperty.AddOwner(
typeof (UserControl1), new FrameworkPropertyMetadata() {Inherits = true});
public UserControl1()
{
InitializeComponent();
}
public int Some
{
get { return (int) GetValue(SomeProperty); }
set { SetValue(SomeProperty, value); }
}
}
我在UserControl1中也创建了属性SomProperty和包装器,但是我没有注册新的Dependency属性,而是将AddOwner添加到SomeControl的属性中。
对我们样本的测试很少。我们在MainWindow上的按钮点击事件:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
UserControl1.Some++;
}
结果我们可以看到,单击后,Label和SomeControl上的值发生了变化。