我有一个字符串依赖属性(SearchText),更新后,需要更新集合依赖属性(结果).
我的收藏dp:
public IEnumerable<string> Results{
get { return (IEnumerable<string>) GetValue(ResultsProperty); }
set { SetValue(ResultsProperty, value); }
}
public static readonly DependencyProperty ResultsProperty=
DependencyProperty.Register("Results", typeof(IEnumerable<string>), typeof(MainWindowVM), new UIPropertyMetadata(new List<string>()));
Run Code Online (Sandbox Code Playgroud)
我试了这个没有运气.我在结果= ....线上放了一个断点,它从未被击中.
public string SearchText{
get { return (string) GetValue(SearchTextProperty); }
set {
Results =
from T in Tree.GetPeople(value)
select T.FullName;
SetValue(SearchTextProperty, value);
}
}
public static readonly DependencyProperty SearchTextProperty=
DependencyProperty.Register("SearchText", typeof(string), typeof(MainWindowVM), new UIPropertyMetadata(""));
Run Code Online (Sandbox Code Playgroud)
XAML:
<TextBox DockPanel.Dock="Top" Text="{Binding SearchValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<ListBox DockPanel.Dock="Top" ItemsSource="{Binding NameResults}" SelectedItem="{Binding Search}" />
我正在使用具有多个用户定义的依赖属性的自定义控件。我遇到了这个问题中描述的相同问题。
我的控件正在其构造函数中设置自定义依赖项属性的默认值。当我在 a 中使用控件时DataTemplate,始终使用构造函数中设置的值,即使我尝试在 XAML 中设置它也是如此。
链接问题的答案解释了 C# 代码中设置的值具有更高的优先级,更好的方法是在依赖项属性的元数据中指定默认值。
就我而言,我无法指定默认值,因为依赖项属性没有适用于所有情况的单个默认值。默认值取决于另一个属性,因此我必须在创建控件时而不是在注册属性时查找它们。
这是一些代码来帮助说明我的问题:
public partial class MyControl : UserControl
{
public static readonly DependencyProperty MyProperty =
DependencyProperty.Register(
"MyProperty",
typeof(int),
typeof(MyControl),
new FrameworkPropertyMetadata(
int.MinValue,
FrameworkPropertyMetadataOptions.None,
new PropertyChangedCallback("OnMyPropertyChanged")));
public MyControl() : base()
{
InitializeComponent();
this.MyProperty = GetDefaultPropertyValue();
}
public int MyProperty
{
get { return (int)GetValue(MyProperty); }
set { SetValue(MyProperty, value); }
}
private int GetDefaultPropertyValue()
{
// look up the appropriate default based on some other criteria
return 42; …Run Code Online (Sandbox Code Playgroud) 这是基于前一个问题的答案的跟进.我设法提出了一个DependencyProperty,它将使用Timer更新,以便始终拥有最新的日期时间,以及一个显示日期时间的文本块.由于它是DependencyProperty,每当定时器更新值时,textblock也会显示最新的DateTime.
依赖对象
public class TestDependency : DependencyObject
{
public static readonly DependencyProperty TestDateTimeProperty =
DependencyProperty.Register("TestDateTime", typeof(DateTime), typeof(TestDependency),
new PropertyMetadata(DateTime.Now));
DispatcherTimer timer;
public TestDependency()
{
timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.DataBind, new EventHandler(Callback), Application.Current.Dispatcher);
timer.Start();
}
public DateTime TestDateTime
{
get { return (DateTime)GetValue(TestDateTimeProperty); }
set { SetValue(TestDateTimeProperty, value); }
}
private void Callback(object ignore, EventArgs ex)
{
TestDateTime = DateTime.Now;
}
}
Run Code Online (Sandbox Code Playgroud)
窗口Xaml
<Window.DataContext>
<local:TestDependency/>
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding TestDateTime}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
这非常有效,但我想知道,如果我想以不同的方式格式化时间字符串,我该怎么办?有没有办法ToString(formatter)在显示文本块之前调用日期时间,同时保持能否使用DependencyProperty自动更新文本块?在后面的代码中执行此操作的正确方法是什么,以及在Xaml中执行此操作的正确方法,如果可能的话?
而且,如果我有多个文本框显示,每个文本框都有不同的日期时间格式,使用只有1个计时器在不同的文本框中显示所有不同的日期时间格式的正确方法是什么,我是否必须创建一个DependencyProperty for每种格式?
我有一个自定义控件与下面的绑定
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<me:MarkdownEditor
Options="{Binding Path=Options, RelativeSource={RelativeSource AncestorType=Window}}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我发现Window1.Options正在设置binding()(在调试模式中单步执行代码之后),markdown编辑器选项(应该设置字体,颜色等)没有设置,或者至少UI不会更新.我想知道发生了什么,MarkdownEditor.xaml.cs但那是另一个(引用)项目.如何确认MarkdownEditor.Options至少设置了?
我实际上已经测试过这MarkdownEditor方面的工作如下
<Window ...>
<Grid>
<Button Content="Options" Click="Button_Click" Grid.Row="0" />
<me:MarkdownEditor Options="{Binding Options, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Row="1" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
所以区别在于后者MarkdownEditor只是一个Grid中的一个Window.失败的是MarkdownEditor一个TabControl受限制的ObservableCollection<TabViewModel>
Visual Studio解决方案复制问题
我不是很擅长解释事情,所以我编写的一个简单的项目减去了上传到媒体大火的所有不必要的噪音,所以你可以看看什么是错的
该视频显示了Screenr上的问题
只需一个简单的用法,窗口/网格中的编辑器.

绑定工作正常
然后,当与TabControl绑定结合使用时ObservableCollection<EditorTabViewModel>,绑定的工作方式如2 TextBoxes更新其值.但编辑器没有更新
