- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这两个 Functions in x:Bind 上都工作过(在 Windows 10 build 14393 中引入)和 IValueConverter将转换后的值绑定(bind)到 UI 元素的属性。但是,我想知道绑定(bind)值的正确或有效程序。使用它们有什么区别。
示例:您可以使用 x:Bind 和 IValueConverter 中的函数将字符串绑定(bind)到“日历选择器”。但是,哪个是有效的?
1.x:Bind中的函数
//Xaml
<CalendarDatePicker Date="{x:Bind ConvertStringToDate(Date),Mode=OneWay}"></CalendarDatePicker>
public DateTimeOffset ConvertStringToDate(string date)
{
DateTime d;
d = System.Convert.ToDateTime(date);
d = DateTime.SpecifyKind(d, DateTimeKind.Local);
return (DateTimeOffset)d;
}
<CalendarDatePicker Date="{x:Bind Date,Converter={StaticResource StringtoDate},Mode=OneWay}"></CalendarDatePicker>
public class DateToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, string language)
{
DateTime d = DateTime.Now;
string date = (string)value;
d = System.Convert.ToDateTime(date);
d = DateTime.SpecifyKind(d, DateTimeKind.Local);
return (DateTimeOffset)d;
}
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
//blah blah
}
}
最佳答案
实际区别在于参数数量和易用性,如 the doc 中所述。 :
- A simpler way to achieve value conversion
- A way for bindings to depend on more than one parameter
x:Bind ConvertStringToDate(Date)
在
x:Bind
这比
IValueConverter
更容易和漂亮的转换值的方法
关于uwp - 在 x :Bind and a IValueConverter? 中使用函数有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796413/
我正在尝试将枚举转换为画笔,以便可以使用枚举变量来控制某些控件的颜色 我的枚举(并不真正相关): public enum Colors { Red, Blue, } 这是我的转
我正在使用 .NET 3.5 我有一个 DataGridTextColumn,我想在该列的值为 false 时将背景颜色变为红色。我已经在 XMAL 中看到了这一点,但无法弄清楚如何在代码隐藏中做到这
我有一个 Enum 需要显示在 ComboBox 中。我已经设法使用 ItemsSource 将枚举值获取到组合框,并且我正在尝试将它们本地化。我认为这可以使用值转换器来完成,但由于我的枚举值已经是字
在谷歌上搜索这个问题几个小时,但看不出我哪里出错了。 我有以下转换器,它只返回 Brushes.Red(也尝试过 Colors.Red),但仍然没有成功。 public class ColorConv
我正在尝试创建一个 IValueConverter,它接受一个 enum 并吐出一个 URI。转换器确实按预期在运行时工作。然而,XAML Designer 给我一个错误提示: Object must
我有一个简单的 IValueConverter,它只使用 TypeConverter 进行转换。但是,在某些情况下,提供的 TypeConverter 会失败。 如果转换器未提供 Binding,我想
我有一个具有 3 种通信状态的应用程序:已连接、已断开连接和待处理。通信状态由一些其他参数控制。我想在 IValueConverter 控制的屏幕上显示相应的图像。但我无法让它工作。 这是我包含 3
我有一个 ListView,它有一个包含两列和许多行的 Grid。每行在每一列中都有一个 TextBlock,每个 Text 属性都绑定(bind)到 ListView 的 ItemSource 中的
我有一个包含大约 12 个项目的解决方案,其中一个设置为启动项目,其中包含主窗口。目前我所有的 IValueConverter 都在这个项目中。 我现在想将它们移动到一个单独的项目中,该项目将只包含转
IValueConverter 的最佳实践是什么? : 可以将 Exception 放在 Convert 方法中还是应该返回“something”? 这是一个例子: [ValueConversion(
我正在使用自定义 DateTimeToString :IValueConverter 在我的 ConvertBack 方法中,当转换失败时我抛出异常,但是它没有显示为验证失败(这是一个未处理的应用程序
我有一个异步方法,我想在 IValueConverter 中触发它。 有没有比通过调用 Result 属性强制它同步更好的方法? public async Task Convert(object va
我有一个进度条,我想根据 bool 值更改颜色; true 为绿色,false 为红色。我的代码看起来应该可以工作(当我将它绑定(bind)到文本框时它返回正确的值),但当它是进度条的颜色属性时却不行
我正在尝试使用 IValueConverter 将集合转换为代理对象以进行数据绑定(bind)。 转换器似乎工作正常,但问题是在集合中添加或删除新对象时。 View 中没有刷新相同的.. 模型对象:
我为自定义控件创建了一个 .cs 类,其中包含以下属性: //Property which is defining the unit of the textblock in the Ringsli
有谁知道是否可以对基于 IValueConverter 的类进行数据绑定(bind)? 我有以下转换器: [ValueConversion(typeof(int), typeof(Article))]
好吧,(在写了几次之后)发现System.Windows.Controls命名空间中已经有一个BooleanToVisibilityConverter,这真是一个惊喜。 可能还有更多这样隐藏的节省时间
我有这个代码: namespace Test { public partial class SearchField : UserControl { public Sea
我需要在转换器类中定义 DependencyProperty,因为我需要此数据来进行转换,并且此数据位于另一个对象中,而不是我绑定(bind)到的对象中。 我的转换器类如下: public class
我有这个代码: namespace Test { public partial class SearchField : UserControl { public Sea
我是一名优秀的程序员,十分优秀!