我以前把枚举绑定到单选按钮,我一般都明白它是如何工作的.我使用了这个问题的替代实现:如何将RadioButtons绑定到枚举?
我想生成一个自定义类型的运行时枚举集,而不是枚举,而是将它们作为一组单选按钮呈现.我已经得到了一个视图,它使用一个运行时枚举的集合ListView,并绑定到ItemsSource和SelectedItem属性,因此我ViewModel可以正确连接.现在我正试图从a切换ListView到a ItemsControl与单选按钮.
就我而言:
<Window.Resources>
<vm:InstanceToBooleanConverter x:Key="InstanceToBooleanConverter" />
</Window.Resources>
<!-- ... -->
<ItemsControl ItemsSource="{Binding ItemSelections}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:ISomeType}">
<RadioButton Content="{Binding Name}"
IsChecked="{Binding Path=SelectedItem, Converter={StaticResource InstanceToBooleanConverter}, ConverterParameter={Binding}}"
Grid.Column="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
InstanceToBooleanConverter具有与EnumToBooleanConverter其他问题相同的实现.这似乎是正确的,因为它似乎只是调用Equals方法:
public class InstanceToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) …Run Code Online (Sandbox Code Playgroud) 只是想知道是否可以仅在禁用的项目上显示WPF (而不是在项目启用时).
我想向用户提供一个工具提示,解释当前禁用某个项目的原因.
我有一个IValueConverter反转布尔IsEnabled属性绑定.但它似乎不适用于这种情况.该ToolTipIS同时显示,当项目被启用和禁用.
因此可以将ToolTip.IsEnabled属性专门绑定到项目自己的属性!IsEnabled?
我想这是一个非常直截了当的问题,但无论如何代码示例:
public class BoolToOppositeBoolConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
#endregion …Run Code Online (Sandbox Code Playgroud) 有没有办法在使用DynamicResource扩展时定义转换器?有些东西
<RowDefinition Height="{Binding Source={DynamicResource someHeight}, Converter={StaticResource gridLengthConverter}}" />
Run Code Online (Sandbox Code Playgroud)
不幸的是,这给了我以下的重复:
无法在"绑定"类型的"Source"属性上设置"DynamicResourceExtension".'DynamicResourceExtension'只能在DependencyObject的DependencyProperty上设置.
我有一个文本框绑定到具有类型Timespan属性的类,并编写了一个值转换器将字符串转换为TimeSpan.
如果在文本框中输入非数字,我希望显示自定义错误消息(而不是默认的"输入字符串格式错误").
转换器代码是:
public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
try
{
int minutes = System.Convert.ToInt32(value);
return new TimeSpan(0, minutes, 0);
}
catch
{
throw new FormatException("Please enter a number");
}
}
Run Code Online (Sandbox Code Playgroud)
我在XAML绑定中设置了'ValidatesOnExceptions = True'.
但是,我遇到了以下MSDN文章,它解释了为什么以上内容不起作用:
"数据绑定引擎不会捕获由用户提供的转换器抛出的异常.Rinit方法抛出的任何异常,或者Convert方法调用的方法抛出的任何未捕获的异常都被视为运行时错误"
我已经读过'ValidatesOnExceptions确实捕获TypeConverters中的异常,所以我的具体问题是:
在WPF应用程序中声明转换器时,我应该:
<Application.Resources/>以便它可供整个应用程序使用Page/ Window/ ResourceDictionary/ UserControl等所需的转换器Resources关于可读性,方法1对我来说似乎是最好的,但我的问题是关于性能.哪种方法在性能,内存等方面最具资源效率?
据我所知,我需要创建一个IValueConverter自定义格式.问题是我的格式是单向的,没有合法的ConvertBack实现.
那么,我该如何处理单向问题IValueConverter.(如果有帮助,这是一个只读控件.)
注意:这是对早期设计的修订,其具有不可用于样式的限制,相当于否定其有效性.但是,这个新版本现在可以使用样式,基本上可以让你在任何可以使用绑定或动态资源的地方使用它,并获得预期的结果,使它非常有用.
从技术上讲,这不是一个问题.这是一篇文章,展示了我发现轻松使用转换器DynamicResource作为源的方式,但为了遵循s/o的最佳实践,我将其作为问题/答案对发布.所以我在下面找到了如何做到这一点的方法,看看我的答案.希望能帮助到你!
我有一个很慢的WPF转换器(计算,在线提取等).如何异步转换以便我的UI不会冻结?我发现这一点,但解决的办法是将转换器代码在属性- http://social.msdn.microsoft.com/Forums/pl-PL/wpf/thread/50d288a2-eadc-4ed6-a9d3-6e249036cb71 -这我宁愿不这样做.
以下是演示该问题的示例.这里的下拉列表将冻结,直到睡眠过去.
namespace testAsync
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyNumbers = new Dictionary<string, int> { { "Uno", 1 }, { "Dos", 2 }, { "Tres", 3 } };
this.DataContext = this;
}
public Dictionary<string, int> MyNumbers
{
get { return (Dictionary<string, int>)GetValue(MyNumbersProperty); }
set { SetValue(MyNumbersProperty, value); }
}
public …Run Code Online (Sandbox Code Playgroud) 我在网上看到了两种不同的方法来增强IValueConverter.其中一个从MarkupExtension扩展了ValueConverter,另一个从DependencyObject扩展.我无法从两者延伸,所以我想知道是否有一个比另一个好?
我刚刚在我实现的IValueConverter上遇到了这个异常:
IValueConverter类型没有公共TypeConverter类
还有其他人遇到过这个吗?是什么原因,我该如何解决?谢谢.
ivalueconverter ×10
wpf ×10
c# ×4
binding ×2
data-binding ×2
mvvm ×1
performance ×1
radio-button ×1
tooltip ×1