【发布时间】:2010-11-17 08:19:50
【问题描述】:
背景
我所拥有的摘要是一个 UserControl MarkdownEditor。它包含一个TextBox,它具有由绑定控制的FontFamily、Background 等显示属性。它从 MarkdownEditorOptions 类中获取这些值,该类包含每个值的属性。我的代码如下所示
在 ShellView 中,展示了如何在我的MarkdownEditor 中为TextBox 设置显示选项
<me:MarkdownEditor>
<me:MarkdownEditor.Options>
<me:MarkdownEditorOptions Background="Red" />
</me:MarkdownEditor.Options>
</me:MarkdownEditor>
在MarkdownEditor.xaml.cs,MarkdownEditor(UserControl)的DataContext,Options的声明
public MarkdownEditorOptions Options
{
get { return (MarkdownEditorOptions)GetValue(OptionsProperty); }
set { SetValue(OptionsProperty, value); }
}
public static readonly DependencyProperty OptionsProperty =
DependencyProperty.Register("Options", typeof(MarkdownEditorOptions), typeof(MarkdownEditor), new UIPropertyMetadata(new MarkdownEditorOptions()));
在 MarkdownEditor.xaml 中:显示TextBox 绑定到选项值的方式
<TextBox Grid.Row="1" x:Name="txtEditor" AcceptsReturn="True" Text="{Binding Path=Content, UpdateSourceTrigger=PropertyChanged}"
FontFamily="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}"
FontSize="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontSize}"
FontWeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontWeight}"
Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.Background, Converter={StaticResource colorToBrushConverter}}"
Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.Foreground, Converter={StaticResource colorToBrushConverter}}" />
MarkdownEditorOptions 类
// MarkdownEditorOptions
public class MarkdownEditorOptions : ObservableObject
{
protected FontFamily _fontFamily;
protected int _fontSize;
protected FontWeight _fontWeight;
protected Color _background;
protected Color _foreground;
// Constructor, for default options
public MarkdownEditorOptions()
{
_fontFamily = new FontFamily("Consolas");
_fontSize = 14;
_fontWeight = FontWeights.Bold;
_background = new Color { R = 32, G = 32, B = 32, A = 255 };
_foreground = new Color { R = 255, G = 255, B = 255, A = 255 };
}
public FontFamily FontFamily {
get { return _fontFamily; }
set {
_fontFamily = value;
RaisePropertyChanged("FontFamily");
}
}
public int FontSize
{
get { return _fontSize; }
set {
_fontSize = value;
RaisePropertyChanged("FontSize");
}
}
public FontWeight FontWeight
{
get { return _fontWeight; }
set {
_fontWeight = value;
RaisePropertyChanged("FontWeight");
}
}
public Color Background
{
get { return _background; }
set {
_background = value;
RaisePropertyChanged("Background");
}
}
public Color Foreground
{
get { return _foreground; }
set {
_foreground = value;
RaisePropertyChanged("Foreground");
}
}
}
问题
我在 MarkdownEditor 中的 TextBox 总是显示来自 MarkdownEditorOptions 构造函数的默认值。在我展示的简单 XAML 中,似乎没有应用红色背景。怎么了?
[更新:11 月 17 日:下午 4:25]
一些想法
我认为这与Path=Options.FontSize 有关。也许这个绑定会跟踪Options而不是Options.FontSize的变化?
更新:11 月 19 日
一些观察:如果我在单独的简单窗口中使用控件
<Window ...>
<Window.Resources>
<me:MarkdownEditorOptions FontFamily="Arial" FontWeight="Normal" Background="Red" x:Key="options" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions ... />
<Button Content="Options ..." Grid.Row="0" Click="Button_Click" />
<me:MarkdownEditor Grid.Row="1" Options="{StaticResource options}" x:Name="markdownEditor" />
</Grid>
</Window>
如果我在更复杂的设置中使用它,一切正常。 TabControl 绑定到ObservableCollection<TabViewModel>,失败了
<TabControl ... ItemsSource="{Binding TabsViewSource}" IsSynchronizedWithCurrentItem="True">
我尝试了使用和不使用绑定。 MarkdownEditorOptions 中的设置器似乎已运行,因为我添加了 Debug.WriteLine() 但背景等没有更新。
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<!--<me:MarkdownEditor Options="{Binding RelativeSource={RelativeSource AncestorType={x:Type v:ShellView}}, Path=ViewModel.Options}" />-->
<me:MarkdownEditor>
<me:MarkdownEditor.Options>
<me:MarkdownEditorOptions Background="Red" />
</me:MarkdownEditor.Options>
</me:MarkdownEditor>
</DataTemplate>
【问题讨论】:
标签: c# wpf data-binding