WPF PropertyDependency 绑定到不同类型
在此处键入
userControl 代码
public double Version
{
get { return (double)GetValue(VersionProperty); }
set
{
SetValue(VersionProperty, value);
SetValue(Version_StringProperty, $"Ver{value:0.00}");
}
}
public string Version_String
{
get { return (string)GetValue(Version_StringProperty); }
set
{
SetValue(Version_StringProperty, value);
}
}
public static readonly DependencyProperty VersionProperty = DependencyProperty.Register("Version", typeof(double), typeof(MachineVersionObject));
public static readonly DependencyProperty Version_StringProperty = DependencyProperty.Register("Version_String", typeof(string), typeof(MachineVersionObject));
XAML
<TextBlock Text="{Binding Path=Version_String, ElementName=maachineVersion, UpdateSourceTrigger=PropertyChanged}" FontSize="15" Foreground="#F1F1F1" FontWeight="Bold"
Margin="2 3 2 0" />
如何将双精度类型绑定到依赖属性字符串或将 Version 属性传递到 Version_string 属性?
我尝试将版本属性直接绑定到文本块,它显示了值 “1.36”“2”“2.5”
但我想在前面添加一个“Ver”并使值显示全部2个小数位。
type here
userControl code
public double Version
{
get { return (double)GetValue(VersionProperty); }
set
{
SetValue(VersionProperty, value);
SetValue(Version_StringProperty, quot;Ver{value:0.00}");
}
}
public string Version_String
{
get { return (string)GetValue(Version_StringProperty); }
set
{
SetValue(Version_StringProperty, value);
}
}
public static readonly DependencyProperty VersionProperty = DependencyProperty.Register("Version", typeof(double), typeof(MachineVersionObject));
public static readonly DependencyProperty Version_StringProperty = DependencyProperty.Register("Version_String", typeof(string), typeof(MachineVersionObject));
XAML
<TextBlock Text="{Binding Path=Version_String, ElementName=maachineVersion, UpdateSourceTrigger=PropertyChanged}" FontSize="15" Foreground="#F1F1F1" FontWeight="Bold"
Margin="2 3 2 0" />
How do i bind a double type to a dependency property string or pass the Version property to the Version_string property?
I tried binding the Version Property directly to the textblock and it displayed the values
"1.36" "2" "2.5"
But i would like to add a "Ver" in front and make the values displayed all 2 decimal places.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个粗略的草图(我认为)可以满足您的要求。神奇之处在于绑定属性的 setter/getter 方法,它利用 INotifier 中的 Update 方法来更新彼此和 GUI,反之亦然。
我使用了 2 个文本框(首先更改版本号),因为这样制作快速 GUI 更简单,但原则上我认为这就是您正在寻找的:
.NET 6、Visual Studio WPF应用程序模板:
MainWindow.xaml
:MainWindow.xaml.cs
(注意,ViewModel
类应该是它自己的文件):最终细节,我修改了< code>App.xaml 和
App.xaml.cs
以便将数据上下文正确连接到 guixaml:
代码隐藏:
Here is a rough sketch that (I think) does what you want. The magic is in the setter/getter method of the bound properties, which utilize the
Update
method inINotifier
to update each other and the GUI and vice versa.I used 2 text boxes (first changes the version number) because it simpler to make a quick GUI that way, but in principle I think this is what you are looking for:
.NET 6, Visual Studio WPF Application template:
MainWindow.xaml
:MainWindow.xaml.cs
(note,ViewModel
class should be its own file):Final detail, I modified
App.xaml
andApp.xaml.cs
in order to wire the data context to the gui properlyxaml:
Code behind: