gpt4 book ai didi

c# - IValueConverter 和可见性

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:43 25 4
gpt4 key购买 nike

我有一个具有 3 种通信状态的应用程序:已连接、已断开连接和待处理。通信状态由一些其他参数控制。我想在 IValueConverter 控制的屏幕上显示相应的图像。但我无法让它工作。

这是我包含 3 张图片的 Xaml 代码:

<Image x:Name="connectedImage"  
Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterConnected}"
Source="Assets/connected.png"
Stretch="None"
HorizontalAlignment="Center" />



<Image x:Name="disconnectedImage"
Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterDisconnected}"
Source="Assets/disconnect.png"
Stretch="None"
HorizontalAlignment="Center" />


<Image x:Name="pendingImage"
Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterPending}"
Source="Assets/pending.png"
Stretch="None"
HorizontalAlignment="Center" />

这里是控制CommunitationState的方法

public enum CommunitationState { Connected, Disconnected, Pending }

public CommunitationState ConnectionWithServerEstablished
{
get
{
if (IRCommandSent)
return CommunitationState.Disconnected;

if (wifiConnected && !fcConnected)
return CommunitationState.Pending;

return wifiConnected ? CommunitationState.Connected : CommunitationState.Disconnected;
}
}

最后但同样重要的是转换器:

  public class CommunitationStateToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var result = Visibility.Collapsed;

if ((string)parameter == "ConverterParameterConnected")
result = (CommunitationState)value == CommunitationState.Connected ? Visibility.Visible : Visibility.Collapsed;

if ((string)parameter == "ConverterParameterDisconnected")
result = (CommunitationState)value == CommunitationState.Disconnected ? Visibility.Visible : Visibility.Collapsed;

if ((string)parameter == "ConverterParameterPending")
result = (CommunitationState)value == CommunitationState.Pending ? Visibility.Visible : Visibility.Collapsed;
Debug.WriteLine("value={0}, parameter={1}, result={2}", value, (string)parameter, result);
return result;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}

数据绑定(bind)工作正常。我知道肯定是因为我有一个文本框绑定(bind)了另一种将状态显示为文本的方法。我的转换器被调用,我肯定知道这一点,因为我可以在其中放置一个断点。

所以我的转换器出了点问题,因为我总是以折叠的图像结束。

**** 编辑 ****

这是我的 debug.Writeline 的一些输出

我启动连接:

value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed

我改为pending:

value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible

我启动挂起:

value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed
value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible

这是正确的,因为我的程序默认连接,一秒钟后它意识到它无法看到 TCP 服务器但仍然可以访问 Wifi,所以我将状态更改为挂起。

最佳答案

根据您的评论,很可能是您的 ConnectionWithServerEstablished 属性没有更改以使图像可见和/或您没有触发 PropertyChanged 事件值(value)改变了。

例如,您可以通过在依赖属性的 setter 中触发事件来执行此操作:

public bool IRCommandSent
{
set
{
// set the value
// ...

// notify event listeneers that the ConnectionWithServerEstablished may have changed
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ConnectionWithServerEstablished"));
}
}
}

您用作 DataContext(您的 ViewModel)的类当然必须为此实现 INotifyPropertyChanged

关于c# - IValueConverter 和可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26924718/

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