我可以通过 ListView 项在 WPF 中使用 ImageAwesome 绑定图标吗?
Can I make an icon binding with ImageAwesome in WPF via a ListView item?
我使用“FontAwesome.WPF”nuget。
这是 ListView 对象的样子
public class Item
{
public string LocalizationNameVariable { get; set; }
public string LocalizationDescriptionVariable { get; set; }
public FontAwesomeIcon IsAlertToggleActive { get; set; }
}
这是 ListView 列当前的样子:
<GridViewColumn Header="{Binding Translation.IsAlertActive, FallbackValue=IS_ALERT_ACTIVE}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Height="24" Width="24">
<fa:ImageAwesome Visibility="Visible" Icon="ToggleOff" MouseUp="AlertModeAlertActiveToggle_MouseUp" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
我在 TextBox 中使用以下内容,这里的值来自 ListView 并存储在有效的项目中。
<TextBox d:DataContext="{d:DesignInstance models:Item}" Text="{Binding (models:Item.AlertModeMinSellPriceIsUndercutPrice)}" MaxLength="12" Height="24" Width="65" PreviewTextInput="TxtBoxMinSellPriceIsUndercutPrice_OnPreviewTextInput"></TextBox>
但我希望能够在两个方向上更改切换图标的值。
我必须使用 PropertyChanged 还是不能使用 FontAwesome?
您应该始终在任何视图模型上实现 inotifypropertychanged。
项目是一个视图模型。
如果你看过fontawesome md,它会指出示例项目中有一个绑定图像的示例。
主窗口在这里:
<fa:FontAwesome
Icon="{Binding Path=Icon}"
FontSize="{Binding ElementName=FontSizeSlider, Path=Value}"
VerticalAlignment="Center" Margin="0,0,10,0" />
视图模型在这里:
public class MainViewModel
{
public ObservableCollection<IconDescription> Icons { get; set; }
public MainViewModel()
{
Icons = new ObservableCollection<IconDescription>();
var icons = Enum.GetValues(typeof (FontAwesomeIcon)).Cast<FontAwesomeIcon>()
.Where(t => t != FontAwesomeIcon.None)
.OrderBy(t => t, new IconComparer());
foreach (var icon in icons)
{
var memberInfo = typeof(FontAwesomeIcon).GetMember(icon.ToString()).FirstOrDefault();
if(memberInfo == null) continue; // alias
foreach (var cat in memberInfo.GetCustomAttributes(typeof(IconCategoryAttribute), false).Cast<IconCategoryAttribute>())
{
var desc = memberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>().First();
var id = memberInfo.GetCustomAttributes(typeof(IconIdAttribute), false).Cast<IconIdAttribute>().FirstOrDefault();
Icons.Add(new IconDescription { Category = cat.Category, Description = desc.Description, Icon = icon, Id = id == null ? null : id.Id });
}
}
}
public class IconComparer
: IComparer<FontAwesomeIcon>
{
public int Compare(FontAwesomeIcon x, FontAwesomeIcon y)
{
return String.Compare(x.ToString(), y.ToString(), System.StringComparison.InvariantCultureIgnoreCase);
}
}
}
public class IconDescription
{
public string Description { get; set; }
public FontAwesomeIcon Icon { get; set; }
public string Category { get; set; }
public string Id { get; set; }
}
我使用“FontAwesome.WPF”nuget。
这是 ListView 对象的样子
public class Item
{
public string LocalizationNameVariable { get; set; }
public string LocalizationDescriptionVariable { get; set; }
public FontAwesomeIcon IsAlertToggleActive { get; set; }
}
这是 ListView 列当前的样子:
<GridViewColumn Header="{Binding Translation.IsAlertActive, FallbackValue=IS_ALERT_ACTIVE}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Height="24" Width="24">
<fa:ImageAwesome Visibility="Visible" Icon="ToggleOff" MouseUp="AlertModeAlertActiveToggle_MouseUp" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
我在 TextBox 中使用以下内容,这里的值来自 ListView 并存储在有效的项目中。
<TextBox d:DataContext="{d:DesignInstance models:Item}" Text="{Binding (models:Item.AlertModeMinSellPriceIsUndercutPrice)}" MaxLength="12" Height="24" Width="65" PreviewTextInput="TxtBoxMinSellPriceIsUndercutPrice_OnPreviewTextInput"></TextBox>
但我希望能够在两个方向上更改切换图标的值。
我必须使用 PropertyChanged 还是不能使用 FontAwesome?
您应该始终在任何视图模型上实现 inotifypropertychanged。
项目是一个视图模型。
如果你看过fontawesome md,它会指出示例项目中有一个绑定图像的示例。
主窗口在这里:
<fa:FontAwesome
Icon="{Binding Path=Icon}"
FontSize="{Binding ElementName=FontSizeSlider, Path=Value}"
VerticalAlignment="Center" Margin="0,0,10,0" />
视图模型在这里:
public class MainViewModel
{
public ObservableCollection<IconDescription> Icons { get; set; }
public MainViewModel()
{
Icons = new ObservableCollection<IconDescription>();
var icons = Enum.GetValues(typeof (FontAwesomeIcon)).Cast<FontAwesomeIcon>()
.Where(t => t != FontAwesomeIcon.None)
.OrderBy(t => t, new IconComparer());
foreach (var icon in icons)
{
var memberInfo = typeof(FontAwesomeIcon).GetMember(icon.ToString()).FirstOrDefault();
if(memberInfo == null) continue; // alias
foreach (var cat in memberInfo.GetCustomAttributes(typeof(IconCategoryAttribute), false).Cast<IconCategoryAttribute>())
{
var desc = memberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>().First();
var id = memberInfo.GetCustomAttributes(typeof(IconIdAttribute), false).Cast<IconIdAttribute>().FirstOrDefault();
Icons.Add(new IconDescription { Category = cat.Category, Description = desc.Description, Icon = icon, Id = id == null ? null : id.Id });
}
}
}
public class IconComparer
: IComparer<FontAwesomeIcon>
{
public int Compare(FontAwesomeIcon x, FontAwesomeIcon y)
{
return String.Compare(x.ToString(), y.ToString(), System.StringComparison.InvariantCultureIgnoreCase);
}
}
}
public class IconDescription
{
public string Description { get; set; }
public FontAwesomeIcon Icon { get; set; }
public string Category { get; set; }
public string Id { get; set; }
}