我有一個用戶控件,我希望通過它的父級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