C# 为什么绑定没有更新 背景
我的总结是一个UserControlC# 为什么绑定没有更新 背景,c#,wpf,data-binding,C#,Wpf,Data Binding,我的总结是一个UserControlMarkdownEditor。它包含一个文本框,该文本框具有由绑定控制的显示属性,如FontFamily、Background等。它从一个MarkdownEditorOptions类中获取这些值,该类包含每个值的属性。我的代码如下所示 在外壳视图中,显示如何在我的标记编辑器中设置文本框的显示选项 <me:MarkdownEditor> <me:MarkdownEditor.Options> <me:Mark
MarkdownEditor
。它包含一个文本框
,该文本框具有由绑定控制的显示属性,如FontFamily
、Background
等。它从一个MarkdownEditorOptions
类中获取这些值,该类包含每个值的属性。我的代码如下所示
在外壳视图中,显示如何在我的标记编辑器中设置文本框的显示选项
<me:MarkdownEditor>
<me:MarkdownEditor.Options>
<me:MarkdownEditorOptions Background="Red" />
</me:MarkdownEditor.Options>
</me:MarkdownEditor>
在MarkdownEditor.xaml
中:显示文本框
绑定选项值的方式
<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}}" />
问题
MarkdownEditor中的我的文本框总是显示MarkdownEditorOptions
的构造函数的默认值。在我展示的简单XAML中,似乎没有应用红色背景。怎么了
[更新:11月17日:下午4:25]
一些想法
我认为这与Path=Options.FontSize
有关。可能此绑定将跟踪对选项的更改,而不是选项。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>
我试过带和不带绑定。正如我添加的Debug.WriteLine()
一样,MarkdownEditorOptions
中的setter似乎正在运行,但是后台etc没有更新
<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>
通常这是由于路径不正确造成的。不幸的是,它不会引发异常,您可以尝试将vs debugger附加到您的应用程序,并检查调试日志中是否存在任何绑定错误这在StackOverflow上我的另一个问题中得到了修复:嗯,我在输出窗口中没有看到任何错误,这是我应该查看的地方吗?事实上,我看到我的setter正在运行,因为我向它们添加了Debug.WriteLine
<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 ... ItemsSource="{Binding TabsViewSource}" IsSynchronizedWithCurrentItem="True">
<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>