
我知道我可能只是在对象中添加一个“WindowTitle”属性,但这看起来相当Hacky.我已经创建了一个非常精简的版本,我正在尝试做的事情.
这是XAML:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" title="{Binding Converter={StaticResource windowTitleConverter}}" Height="195" WIDth="245"><Window.Resources> <local:WindowTitleConverter x:Key="windowTitleConverter"/></Window.Resources><GrID Height="150" WIDth="217"> <TextBox Height="23" HorizontalAlignment="left" margin="12,12,0" name="textBox1" VerticalAlignment="top" WIDth="120" Text="{Binding filename}" /> <CheckBox Content="ModifIEd" Height="16" HorizontalAlignment="left" margin="12,41,0" name="checkBox1" VerticalAlignment="top" IsChecked="{Binding ModifIEd}" /></GrID> 和守则:
using System;using System.Globalization;using System.Text;using System.windows;using System.windows.Controls;using System.windows.Data;namespace WpfApplication1{ public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new Foo(); } } public class Foo : DependencyObject { public string filename { get { return (string)GetValue(filenameProperty); } set { SetValue(filenameProperty,value); } } public static Readonly DependencyProperty filenameProperty = DependencyProperty.Register("filename",typeof(string),typeof(Foo),new UIPropertyMetadata()); public bool ModifIEd { get { return (bool)GetValue(ModifIEdProperty); } set { SetValue(ModifIEdProperty,value); } } public static Readonly DependencyProperty ModifIEdProperty = DependencyProperty.Register("ModifIEd",typeof(bool),new UIPropertyMetadata(0)); } public class WindowTitleConverter : IValueConverter { public object Convert(object value,Type targettype,object parameter,CultureInfo culture) { Foo foo = (Foo)value; if (foo == null || foo.filename == null) return "Foo"; return foo.filename + (foo.ModifIEd ? " *" : ""); } public object ConvertBack(object value,CultureInfo culture) { throw new NotSupportedException(); } }}解决方法 如果你移动资源下面的标题绑定它将工作.我不确定为什么声明的顺序在这里很重要,但它似乎对我来说是个错误 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Height="195" WIDth="245"> <Window.Resources> <local:WindowTitleConverter x:Key="windowTitleConverter"/> </Window.Resources> <Window.Title> <Binding Converter="{StaticResource windowTitleConverter}"/> </Window.Title> <!--...--></Window> 更新
您现在遇到的问题是因为Dependency Property ModifIEd具有错误的默认值类型.它是bool类型,你将它设置为0,所以将其更改为false,它应该工作
public static Readonly DependencyProperty ModifIEdProperty = DependencyProperty.Register("ModifIEd",new UIPropertyMetadata(false)); 更新
在直接绑定到DataContext时,我不知道有任何方法可以引发PropertyChanged.您可以使用的一个小解决方法是绑定到一个名为This的属性,它只返回它
<Window.Title> <Binding Path="This" Converter="{StaticResource windowTitleConverter}"/></Window.Title> 然后,您可以使用PropertyChangedCallback为此引发PropertyChanged
public class Foo : DependencyObject,INotifyPropertyChanged{ public Object This { get { return this; } } public bool ModifIEd { get { return (bool)GetValue(ModifIEdProperty); } set { SetValue(ModifIEdProperty,value); } } public string filename { get { return (string)GetValue(filenameProperty); } set { SetValue(filenameProperty,value); } } public static Readonly DependencyProperty filenameProperty = DependencyProperty.Register("filename",new UIPropertyMetadata(string.Empty,new PropertyChangedCallback(OnfilenameChanged))); public static Readonly DependencyProperty ModifIEdProperty = DependencyProperty.Register("ModifIEd",new UIPropertyMetadata(false,new PropertyChangedCallback(OnModifIEdChanged))); private static voID OnfilenameChanged(DependencyObject obj,DependencyPropertyChangedEventArgs e) { Foo foo = obj as Foo; foo.OnPropertyChanged("This"); } private static voID OnModifIEdChanged(DependencyObject obj,DependencyPropertyChangedEventArgs e) { Foo foo = obj as Foo; foo.OnPropertyChanged("This"); } public event PropertyChangedEventHandler PropertyChanged; public voID OnPropertyChanged(string propertyname) { if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyname)); } }} 另一种解决方案是使用MultiBinding,这样就不需要使用This属性了
<Window.Resources> <local:TitleMultiConverter x:Key="TitleMultiConverter"/></Window.Resources><Window.Title> <MultiBinding Converter="{StaticResource TitleMultiConverter}"> <Binding Path="filename"/> <Binding Path="ModifIEd"/> </MultiBinding></Window.Title> TitleMultiConverter
public class TitleMultiConverter : IMultiValueConverter{ public object Convert(object[] values,CultureInfo culture) { string filename = values[0].ToString(); bool modifIEd = (bool)values[1]; if (filename == null) return "Foo"; return filename + (modifIEd ? " *" : ""); } public object[] ConvertBack(object value,Type[] targettypes,CultureInfo culture) { throw new NotImplementedException(); }} 总结 以上是内存溢出为你收集整理的c# – 使用转换器将Window Title绑定到属性全部内容,希望文章能够帮你解决c# – 使用转换器将Window Title绑定到属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)