我有一些实现INotifyPropertyChanged接口的属性。它工作正常。但是在我的代码中,我也使用了一些值转换器(如果值 < 3 - 将网格设为红色,如果值 >3 且值 < 10 - 将网格设为蓝色等)。
问题是如何在提出PropertyChanged后刷新值转换器?解决方案背后是否有简单的代码?谢谢大家,对不起我的英语不好!
这里有一些代码:
public class NotifyColors : INotifyPropertyChanged
{
private Color _TodayColor;
public Color TodayColor
{
get
{
return _TodayColor;
}
set
{
if (_TodayColor != value)
{
_TodayColor = value;
OnPropertyChanged("TodayColor");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
// it raised correctly when I change color with color picker control
}
} …
Run Code Online (Sandbox Code Playgroud) 有一种情况,UI中的复选框需要绑定到数字参数 - 实际上,某些值使复选框为"true",否则为"false".
最简单的方法似乎是使用转换器:
[ValueConversion(typeof(int), typeof(bool?))]
public class TypeToBoolConverter : IValueConverter
{
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 bool");
if( (value < 3)
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplmentedExpection();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在XAML中:
<CheckBox IsChecked="{Binding Path=Type.TypeID, Converter={StaticResource TypeConverter}}" />
Run Code Online (Sandbox Code Playgroud)
当使用Convert时,它就像魅力一样,但在使用ConvertBack时完全失败,因为它需要知道数值是什么(它依赖于其他UI元素)才能知道要返回的数字 - 实际上它需要访问绑定对象.
我假设我可以使用ConverterParameter执行此操作,但从事物的外观来看,您无法将值绑定到此属性.
有没有办法解决这个烂摊子?
编辑:我已经通过搞乱原始绑定解决了这个问题,并且放弃了它,因为取消选中我想要做的就是删除项目.但是我将把它留在原处,因为它似乎是一个有效的问题,我对可能的解决方案感到好奇.
使用转换器(IValueConverter)和传入其他值作为参数(ConverterParameter)与使用MultiConverter(IMultiValueConverter)并传递多个转换器值之间有什么区别?
有没有办法在Converter类的Convert方法中获取源对象.我知道如何获得转换后的属性但是可以获取属性所属的对象吗?
如果我的Binding.IsAsync = true,那么为什么我的ValueConverter不会在相同的"假设非UI"线程上执行?
有没有办法让它在该线程上执行?
我已经编写了一个转换器类(实现IValueConverter),该类将数据库(例如“ CTY”)中的代码转换为更加用户友好的描述(例如“ City”)。我想在XCeed WPF Datagridcontrol中的单个列上使用转换器,但是我不知道必须将Converter设置为哪个属性。我也尝试使用一种样式将其附加到DataCell上,但是它不能正常工作,并且我认为也是没有必要的,因为转换器应仅应用于一列,而不是应用于每个单元格。
这些列也是自动生成的,因此,如果我可以在运行时应用它们,那将非常棒!
我不知道我必须将该转换器应用到该列的哪个属性(Xceed列没有“ Binding”属性。你们有什么建议吗?
如果需要,可以提供更多示例或代码。希望我的问题对您有所帮助。
编辑:
这是我在XAML文件中使用的东西:
<utils:BudgettaireEntiteitConverter x:Key="BudgettaireEntiteitConverter" />
<xcdg:DataGridCollectionViewSource x:Key="GridViewSourceDefault"
Source="{Binding Converter={StaticResource BudgettaireEntiteitConverter}}">
<xcdg:DataGridCollectionViewSource.DetailDescriptions>
<lc:ActieOverzichtBudgettenDescription
RelationName="Budgetten"
AutoCreateDetailDescriptions="False"
AutoCreateForeignKeyDescriptions="False"
AutoCreateItemProperties="True"
Title="Budgetten" >
<lc:ActieOverzichtBudgettenDescription.StatFunctions>
<xcdg:SumFunction ResultPropertyName="SumOfBedragInBudget"
SourcePropertyName="BedragInBudget" />
<xcdg:SumFunction ResultPropertyName="SumOfBedragInAfwachting"
SourcePropertyName="BedragInAfwachting" />
</lc:ActieOverzichtBudgettenDescription.StatFunctions>
<lc:ActieOverzichtBudgettenDescription.DetailDescriptions>
<lc:ActieBudgetRegistratieSleutelsDescription RelationName="RegistratieSleutels"
AutoCreateDetailDescriptions="False"
AutoCreateForeignKeyDescriptions="False"
AutoCreateItemProperties="True"
Title="Registratiesleutels" />
</lc:ActieOverzichtBudgettenDescription.DetailDescriptions>
</lc:ActieOverzichtBudgettenDescription>
</xcdg:DataGridCollectionViewSource.DetailDescriptions>
</xcdg:DataGridCollectionViewSource>
<xcdg:DataGridControl x:Name="lsvActies"
TargetUpdated="OnListTargetUpdated"
ItemsSourceName="Acties"
IsRefreshCommandEnabled="False"
rf:XceedGridService.LoadUserSettings="True"
rf:XceedGridService.SettingsKeyName="ActieOverzichtGridKey"
rf:XceedGridService.ItemContextMenu="{StaticResource ActieContextMenu}">
<xcdg:DataGridControl.CommandBindings>
<CommandBinding Command="Delete" Executed="ExecuteDeleteItem" CanExecute="CanExecuteDeleteItem"/>
</xcdg:DataGridControl.CommandBindings>
</xcdg:DataGridControl>
Run Code Online (Sandbox Code Playgroud)
这是我的转换器:
Public Class BudgettaireEntiteitConverter
Implements IValueConverter
Private hs As Hashtable = FillHashTable()
Public Function Convert(value …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用IValueConverter.我有以下转换器:
[ValueConversion(typeof(string), typeof(string))]
public class RequiredFieldConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return "";
return value.ToString() + "*";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return "";
var str = value.ToString();
return str+"Convert Back testing";
}
}
Run Code Online (Sandbox Code Playgroud)
我在app.xaml文件中添加了RequiredFieldConverter资源,我想尝试将其作为:
<TextBox Name="textBox2" Width="120" />
<TextBox Text="{Binding ElementName=textBox2, Path=Text, Converter=RequiredFieldConverter}" Name="textBox3" Width="120" />
Run Code Online (Sandbox Code Playgroud)
我希望当我在textbox2中键入"hello"时,它会在textbox3中显示"hello*",但它不起作用.实际上我在运行时遇到以下异常:
{"无法将'System.String'类型的对象强制转换为'System.Windows.Data.IValueConverter'."}
我也知道值转换器功能正在工作,因为它可以在我这样做时工作:
Content="{Binding Source={StaticResource Cliente}, Converter={StaticResource RequiredFieldConverter}}"
Run Code Online (Sandbox Code Playgroud) 基本上我在这里使用了发布的转换器,只是将数据类型更改为通用.如何在XAML中使用通用转换器?
public class ReverseListConverter<T> : MarkupExtension, IValueConverter
{
public ReverseListConverter()
{
}
private ObservableCollection<T> _reversedList;
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
_reversedList = new ObservableCollection<T>();
var data = (ObservableCollection<T>)value;
for (var i = data.Count - 1; i >= 0; i--)
_reversedList.Add(data[i]);
data.CollectionChanged += DataCollectionChanged;
return _reversedList;
}
void DataCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var data = (ObservableCollection<T>)sender;
_reversedList.Clear();
for (var i = data.Count - …
Run Code Online (Sandbox Code Playgroud) 简单的问题:
为什么我必须要constructor
一个IValueConverter
?
我不是在问,为什么我必须要一个empty constructor
,我问为什么我必须要一个?
如果我有:
public class PauseButtonValueConverter : MarkupExtension, IValueConverter
{
//public PauseButtonValueConverter() //<- Uncomment this and the error is fixed
//{
//}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我VS2012
给了我编译时错误:No constructor for type 'PauseButtonValueConverter' has 0 …
我目前正在尝试条件显示图像.我读了很多关于valueConverters和触发器的内容,但我坚信必须有一个更容易解决这个简单问题的方法.
XAML:
<Image Source="C:\Users\Niko\Pictures\red.png" IsEnabled="{Binding IsOn}"></Image>
Run Code Online (Sandbox Code Playgroud)
背后的代码:
namespace MVVM {
public class Globals
{
int i = 2;
public bool IsOn
{
get
{
if (i == 1 )
return true;
else
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我玩了整数i来查看图像是否显示.任何建议都非常令人沮丧!
ivalueconverter ×10
wpf ×9
c# ×5
xaml ×3
binding ×2
.net ×1
asynchronous ×1
data-binding ×1
generics ×1
triggers ×1
vb.net ×1