转换器未挂接到 PropertyChanged 事件
Converter not hooked to PropertyChanged event
我有一个层次结构绑定到 entity framework 模型的树视图。
下面的矩形仅适用于初始加载。并且在我触发 Propertychanged 事件时不会更新其颜色等。
<HierarchicalDataTemplate ItemsSource="{Binding tblLines}">
<StackPanel Orientation="Horizontal" Margin="2">
<Image Height="15" Margin="4" x:Name="imgTreeProject" Source="/DES STUDIO PR;component/Resources/Images/folder_closed.png"/>
<TextBlock Text="{Binding Name}" Margin ="8" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Rectangle Name="X" Width="10" Height="10" Fill="{Binding Path=.,Converter={StaticResource CheckoutConverter},UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding Path=.,Converter={StaticResource CheckoutPersonConverter},UpdateSourceTrigger=PropertyChanged}" ToolTipService.IsEnabled="{Binding Path=.,Converter={StaticResource CheckoutToolTipVis},UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
这是我的转换器:
public class CheckoutConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is TblBase)
{
var Baseentity = value as TblBase;
if (Baseentity.COID == MainWindow.LocalUser.ID)
{
return Brushes.Green;
}
else if ((Baseentity.COID == 0) || (Baseentity.COID == null))
{
return Brushes.Transparent;
}
else if (Baseentity.COID != 0)
{
return Brushes.Black;
}
}
return Brushes.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class CheckoutPersonConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is TblBase)
{
var Baseentity = value as TblBase;
//COID equals other user's ID
if ((Baseentity.COID != 0) && (Baseentity.COID != null) && (Baseentity.COID != MainWindow.LocalUser.ID))
{
var user = DESDatabase.GetUser(Baseentity.COID.Value);
return "Checked out by " + user;
}
else if (Baseentity.COID == MainWindow.LocalUser.ID)
return "You have this node checked out.";
else
return "error";
}
return "error";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class CheckoutToolTipVis : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is TblBase)
{
var Baseentity = value as TblBase;
//COID equals other user's ID
if ((Baseentity.COID != 0) && (Baseentity.COID != null))
{
return true;
}
else
return false;
}
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我的 属性 更改事件确实在此处调用:
private Nullable<int> _coid;
public Nullable<int> COID
{
get
{
NotifyPropertyChanged();
return _coid;
}
set
{
_coid = value;
NotifyPropertyChanged();
}
}
属性 更改的事件适用于不使用转换器的 treeview 绑定,因此我不确定我缺少什么。
转换器 (IValueConverter) 不订阅 PropertyChanged 事件,这是绑定的责任。
当前转换器用于绑定到整个对象,而不是绑定到 属性:
Fill="{Binding Path=.,Converter={StaticResource CheckoutConverter},UpdateSourceTrigger=PropertyChanged}"
所以 COID
的更新未被注意到。
您需要将出价更改为
Fill="{Binding Path=COID,Converter={StaticResource CheckoutConverter},UpdateSourceTrigger=PropertyChanged}"
并更改转换器:
public class CheckoutConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is int?)
{
var COID = value as int?;
if (COID == MainWindow.LocalUser.ID)
{
return Brushes.Green;
}
else if ((COID == 0) || (COID == null))
{
return Brushes.Transparent;
}
else if (COID != 0)
{
return Brushes.Black;
}
}
return Brushes.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我有一个层次结构绑定到 entity framework 模型的树视图。 下面的矩形仅适用于初始加载。并且在我触发 Propertychanged 事件时不会更新其颜色等。
<HierarchicalDataTemplate ItemsSource="{Binding tblLines}">
<StackPanel Orientation="Horizontal" Margin="2">
<Image Height="15" Margin="4" x:Name="imgTreeProject" Source="/DES STUDIO PR;component/Resources/Images/folder_closed.png"/>
<TextBlock Text="{Binding Name}" Margin ="8" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Rectangle Name="X" Width="10" Height="10" Fill="{Binding Path=.,Converter={StaticResource CheckoutConverter},UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding Path=.,Converter={StaticResource CheckoutPersonConverter},UpdateSourceTrigger=PropertyChanged}" ToolTipService.IsEnabled="{Binding Path=.,Converter={StaticResource CheckoutToolTipVis},UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
这是我的转换器:
public class CheckoutConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is TblBase)
{
var Baseentity = value as TblBase;
if (Baseentity.COID == MainWindow.LocalUser.ID)
{
return Brushes.Green;
}
else if ((Baseentity.COID == 0) || (Baseentity.COID == null))
{
return Brushes.Transparent;
}
else if (Baseentity.COID != 0)
{
return Brushes.Black;
}
}
return Brushes.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class CheckoutPersonConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is TblBase)
{
var Baseentity = value as TblBase;
//COID equals other user's ID
if ((Baseentity.COID != 0) && (Baseentity.COID != null) && (Baseentity.COID != MainWindow.LocalUser.ID))
{
var user = DESDatabase.GetUser(Baseentity.COID.Value);
return "Checked out by " + user;
}
else if (Baseentity.COID == MainWindow.LocalUser.ID)
return "You have this node checked out.";
else
return "error";
}
return "error";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class CheckoutToolTipVis : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is TblBase)
{
var Baseentity = value as TblBase;
//COID equals other user's ID
if ((Baseentity.COID != 0) && (Baseentity.COID != null))
{
return true;
}
else
return false;
}
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我的 属性 更改事件确实在此处调用:
private Nullable<int> _coid;
public Nullable<int> COID
{
get
{
NotifyPropertyChanged();
return _coid;
}
set
{
_coid = value;
NotifyPropertyChanged();
}
}
属性 更改的事件适用于不使用转换器的 treeview 绑定,因此我不确定我缺少什么。
转换器 (IValueConverter) 不订阅 PropertyChanged 事件,这是绑定的责任。
当前转换器用于绑定到整个对象,而不是绑定到 属性:
Fill="{Binding Path=.,Converter={StaticResource CheckoutConverter},UpdateSourceTrigger=PropertyChanged}"
所以 COID
的更新未被注意到。
您需要将出价更改为
Fill="{Binding Path=COID,Converter={StaticResource CheckoutConverter},UpdateSourceTrigger=PropertyChanged}"
并更改转换器:
public class CheckoutConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is int?)
{
var COID = value as int?;
if (COID == MainWindow.LocalUser.ID)
{
return Brushes.Green;
}
else if ((COID == 0) || (COID == null))
{
return Brushes.Transparent;
}
else if (COID != 0)
{
return Brushes.Black;
}
}
return Brushes.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}