gpt4 book ai didi

c# - WPF从DataTrigger更改图像

转载 作者:行者123 更新时间:2023-12-03 10:58:54 27 4
gpt4 key购买 nike

首先,值得一提的是,我研究了其他类似的主题,它们帮助我取得了长足的进步,但我需要一点帮助来克服终点。

我遇到的问题是我无法让我的DataTrigger显示正确的图像,当InPossesion bool标志设置为false时,我将枚举属性设置为IconImage2,这反过来又应该更改数据网格中的图像变成红色圆圈,这不会发生。如果有人能给我关于我要去哪里的任何指示,那将很棒。

ViewModel枚举

 public enum  IconEnum
{
IconImage1,
IconImage2
}

public IconEnum MyIconEnumProperty
{
get { return _myEnum; }
set
{
_myEnum = value;
RaisePropertyChanged("MyIconEnumProperty");
}
}

ViewModel方法加载订单
 private void LoadCloakroomOrders()
{
CloakroomOrderRepository repo = new CloakroomOrderRepository();
//Get All orders
var orders = repo.GetPublic();
foreach (var orderItem in orders)
{
Orders.Add(orderItem);
if (orderItem.InPossesion == false)
{
MyIconEnumProperty = IconEnum.IconImage2;
}
}
}

XAML
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Orders}" 
SelectedItem="{Binding Path=SelectedCloakroomOrder}"
Margin="0,23,0,-0.5" Width="980" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Visibility="{Binding ShowIcon,
Converter={StaticResource BooleanToVisibilityConverter},
FallbackValue=hidden}" >
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/Resources/Images/circle_green.png"/>
<Style.Triggers>
<DataTrigger Binding="{Binding MyIconEnumProperty}" Value="IconImage2">
<Setter Property="Source" Value="/Resources/Images/circle_red.png"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

谢谢!

最佳答案

您可以仅具有InPossesion属性,然后基于该属性隐藏/显示两个图像,而不是在VM中具有枚举。这样可以使 View 模型更整洁,并使XAML更清晰:

ViewModel:

public bool InPossession
{
get { return _inPossession; }
set { _inPossion = value; RaisePropertyChanged("InPossession"); }
}

private void LoadCloakroomOrders()
{
CloakroomOrderRepository repo = new CloakroomOrderRepository();
//Get All orders
var orders = repo.GetPublic();
foreach (var orderItem in orders)
{
Orders.Add(orderItem);
if (orderItem.InPossesion == false)
{
InPossession = false;
}
}
}

转换器:
public class BooleanToVisibilityConverter : IValueConverter
{
public Visibility VisibilitIfTrue { get;set; }
public Visibility VisibilitIfFalse { get;set; }

public BooleanToVisibilityConverter()
{
// Set default values for the most common usage
VisibilityIfTrue = Visible;
VisibilityIfFalse = Collapsed;
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{

// Converter could be extended to handle nullable bools as well, but ignore for now
if (value == null)
{
return DependencyProperty.UnsetValue;
}

// value should be of type bool
bool b = (bool)value;
if (b == true)
{
return VisibilityIfTrue;
}
else
{
return VisibilityIfFalse;
}
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException(); // Not necessary
}
}

XAML:
<UserControl>
<UserControl.Resources>
<converters:BooleanToVisibilityConverter x:Key="TrueToVisibleConverter" VisibilityIfTrue="Visible" VisibleIfFalse="Collapsed"/>
<converters:BooleanToVisibilityConverter x:Key="FalseToVisibleConverter" VisibilityIfTrue="Collapsed" VisibleIfFalse="Visible"/>
</UserControl.Resources>
</UserControl>
...
<DataTemplate>
<Grid Visibility="{Binding ShowIcon, FallbackValue=hidden}">
<Image Source="/Resources/Images/circle_green.png" Visibility="{Binding InPossession, Converter={StaticResource TrueToVisibleConverter}}"/>
<Image Source="/Resources/Images/circle_red.png" Visibility="{Binding InPossession, Converter={StaticResource FalseToVisibleConverter}}"/>
</Grid>
</DataTemplate>

关于c# - WPF从DataTrigger更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26589566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com