我有一个用户控件,我希望通过它的父级xaml(本例中是一个wpf页面)进行访问。我的用户控件有一个标签属性,我希望通过将页面xaml绑定到传递到页面xaml的datacontext来设置它。链接依赖属性xaml wpf
在下面的示例中,xaml页面中的数据绑定似乎正常工作。我可以通过页面xaml设置用户控件标签的内容,只要我不尝试将其绑定到页面datacontext,当我尝试这样做时,标签始终显示为空白。
我遵循了本网站上提出的其他一些相关示例,但到目前为止还没有找到我要出错的地方。任何建议将不胜感激。
用户控件XAML
<UserControl x:Class="Pipeline_General.Custom_Controls.AttributeStack"
xmlns:pm="clr-namespace:Pipeline_General"
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"
mc:Ignorable="d"
DataContext = "{Binding RelativeSource={RelativeSource Self}}"
d:DesignHeight="30" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Label Foreground="{x:Static pm:myBrushes.pink}" Content="{Binding Path=Attr}" Grid.Column="0" HorizontalAlignment="Right" FontFamily="Calibri" FontSize="14" Margin="5,0,5,0"/>
<Label x:Name="ValLabel" Foreground="{x:Static pm:myBrushes.blue}" Grid.Column="1" HorizontalAlignment="Left" FontFamily="Calibri" FontSize="14" Margin="5,0,5,0"/>
</Grid>
用户控件代码背后
public partial class AttributeStack : UserControl
{
public static readonly DependencyProperty AttrProperty = DependencyProperty.Register
(
"Attr",
typeof(string),
typeof(AttributeStack),
new PropertyMetadata(string.Empty)
);
public static readonly DependencyProperty ValProperty = DependencyProperty.Register
(
"Val",
typeof(string),
typeof(AttributeStack),
new PropertyMetadata(string.Empty)
);
public static readonly DependencyProperty ValTextProperty = DependencyProperty.Register
(
"ValText",
typeof(string),
typeof(AttributeStack),
new PropertyMetadata(string.Empty)
);
public string Val
{
get { return (string)GetValue(ValProperty); }
set { SetValue(ValProperty, value); }
}
public string ValText
{
get { return (string)GetValue(ValTextProperty); }
set { SetValue(ValTextProperty, value); }
}
public string Attr
{
get { return (string)GetValue(AttrProperty); }
set { SetValue(AttrProperty, value); }
}
public AttributeStack()
{
InitializeComponent();
Binding valBinding = new Binding("Val")
{
Source = Val,
Mode = BindingMode.OneWay
};
Binding textBinding = new Binding("Content")
{
Source = this.ValLabel.Content,
Mode = BindingMode.OneWay
};
ValLabel.SetBinding(Label.ContentProperty, valBinding);
this.SetBinding(AttributeStack.ValTextProperty, textBinding);
}
}
页XAML
<Page x:Class="Pipeline_General.SceneView" x:Name="page"
xmlns:pm="clr-namespace:Pipeline_General"
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"
mc:Ignorable="d"
xmlns:cc="clr-namespace:Pipeline_General.Custom_Controls"
d:DesignHeight="800" d:DesignWidth="600"
DataContext = "{Binding RelativeSource={RelativeSource Self}}"
Title="SceneView">
<Grid>
<ScrollViewer>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Expander IsExpanded="True" Margin="5">
<Expander.Header>
<Grid x:Name="Grid" HorizontalAlignment="Stretch" Width="NaN">
<Border x:Name="Border" Background="Transparent" HorizontalAlignment="Stretch" BorderBrush="{x:Static pm:myBrushes.blue}" BorderThickness="0,0,0,1"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">
<TextBlock x:Name="HeaderText" FontSize ="18" FontFamily="Calibri" Foreground="{x:Static pm:myBrushes.blue}" Text="General Info: " />
</Border>
</Grid>
</Expander.Header>
<StackPanel>
<Separator Foreground="Transparent" Height="0" Margin="5"/>
<cc:AttributeStack Attr="Title:" Val="{Binding Path=DataContext.Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<Separator Foreground="Transparent" Height="0" Margin="5"/>
<cc:AttributeStack Attr="Episode:" Val="{Binding Path=DataContext.episode.Key, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<cc:AttributeStack Attr="Sequence:" Val="{Binding Path=DataContext.sequence.Key, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<cc:AttributeStack Attr="Scene:" Val="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<Separator Foreground="Transparent" Height="0" Margin="5"/>
<cc:AttributeStack Attr="Assigned:" Val="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<cc:AttributeStack Attr="Status:" Val="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<Separator Foreground="Transparent" Height="0" Margin="5"/>
<cc:AttributeStack Attr="Last Saved (Time):" Val="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<cc:AttributeStack Attr="Last Saved (User):" Val="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
<Separator Foreground="Transparent" Height="0" Margin="5"/>
<cc:AttributeStack Attr="Due:" Val="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"/>
</StackPanel>
</Expander>
</StackPanel>
<StackPanel Grid.Column="1">
<cc:Playblast_Viewer x:Name="PlayblastView"/>
<cc:FeedbackCtrl Header="Feedback:"/>
<cc:FeedbackCtrl Header="NoticeBoard:"/>
</StackPanel>
</Grid>
</ScrollViewer>
</Grid>
标签结合DataContext.Title字段似乎是工作,并设置ATTR字段似乎正确工作/作为也在意料之中。因此,它只是Val域,我不能工作..
已更新,其中包括页面代码后面。尽管这里几乎没有任何事情发生。
public partial class SceneView : Page
{
public Scene scene { get; set; }
public SceneView(Scene s)
{
InitializeComponent();
scene = s;
this.DataContext = scene;
}
}
我明白,即时通讯的IM非常漂亮,所以我很有可能尝试做更好的完全不同的方式。在这种情况下,场景对象具有多个使用页面和用户控件进行公开的字段。我编写了完整的应用程序在没有任何xaml的代码背后,但现在我正在尝试自学xaml并让原型的下一个版本更加流畅,并将所需编码减少了80%。
场景对象,它是页面对象的数据上下文,我试图发送字段到用户控件,即。 page.DataContext.episode.Key是代码后面的合法字段。通过上面的xaml我正在寻找它的值发送到Val字段,然后Val字段将更新ValText(用户控件中的标签)内容属性。
链/布线是从不与依赖属性的问题。对我来说,你的逻辑似乎有点纠结。我发现有点难以想象所需。如果你能为相同的东西画出一些东西,我会很感激。即。财产来源,调解人和目标。 – pushpraj 2014-08-30 05:45:30
你能分享页面背后的代码吗? – 2014-08-30 06:05:36
请看这里http://stackoverflow.com/questions/23482734/wpf-chain-binding?rq=1 – Developer 2014-08-30 06:46:00