我的Windows Phone 8 Silverlight应用的每个页面都有一个标题。为了不再重复自己,我决定创建一个简单的UserControl,它包含一个Grid和一个TextBlock。 TextBlock的文本绑定到UserControl(TitleProperty)的DependencyProperty。请参阅最后的代码。
为了使TitleProperty的绑定正常工作,我需要使用代码DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
将UserControl的DataContext绑定到自身。
在页面中使用控件并为Title属性设置固定值或 StaticBinding 时,此功能正常。例:
这有效:
<uiuc:SubSectionHeader Title="My fixed title" />
这适用于
<uiuc:SubSectionHeader Title="{Binding Source={StaticResource Literals}, Path=Literals.applicationSettings, FallbackValue='[Application Settings]'}" />
但是在进行正常绑定时它不起作用。实施例
<uiuc:SubSectionHeader Title="{Binding Path=UserName'}" />
这是因为UserControl在其自己的 DataContext (女巫本身)中寻找 UserName 属性,而不是在页面的DataContext中。
问题1是:在将值传递给Title属性之前,它不应该解析绑定吗?
问题2:它是否只是覆盖了UserControl中 中的绑定?
问题3:如何解决?请不要说创建自定义控件,因为它很糟糕
<UserControl x:Class="UI.UserControls.SubSectionHeader"
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:converters="clr-namespace:UI.Converters"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="50"
d:DesignWidth="480"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<UserControl.Resources>
<converters:StringCaseConverter x:Name="ToUppercase" />
<converters:BooleanToVisibilityConverter x:Key="BoolToVisibility" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Height="50"
Background="{Binding Source={StaticResource ThemeSetter}, Path=BackgroundColor}">
<TextBlock VerticalAlignment="Center"
Margin="25 0 0 0"
Style="{StaticResource PhoneTextNormalStyle}"
Foreground="White"
Text="{Binding Path=Title, Mode=TwoWay, Converter={StaticResource ToUppercase}, ConverterParameter='upper', FallbackValue='[SUB SECTION TITLE]'}" />
</Grid>
</UserControl>
代码背后:
using System.Windows;
using System.Windows.Controls;
namespace UI.UserControls {
public partial class SubSectionHeader : UserControl {
public SubSectionHeader() {
InitializeComponent();
}
public string Title {
get { return (string)GetValue( TitleProperty ); }
set { SetValue( TitleProperty, value ); }
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
"Title",
typeof( string ),
typeof( SubSectionHeader ),
new PropertyMetadata( "[SubSection Title]" )
);
}
}
答案 0 :(得分:-1)
您是否尝试在后面的代码中设置UserControl的DataContext? (你会删除xaml中的setter)
public partial class SubSectionHeader : UserControl {
public SubSectionHeader() {
InitializeComponent();
DataContext = this;
}
编辑:尝试在xaml中设置控件的设计实例。 (也将代码保留在语句后面)
<UserControl
x:Class="MyControl
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"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
d:DataContext="{d:DesignInstance}">