问题
I have an application with 3 comunicationstates: Connected, Disconnected and Pending. The communication state are controled by some other paramers. I want to display the corresponding image on the screen controlled by an IValueConverter. But I cant get it to work.
Heres is my Xaml code for includeing the 3 images :
<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" />
Here is the methos controlling 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;
}
}
And last but not least the converter:
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();
}
}
The Databinding works like its supposed to do. I know that for sure bacause I have a textbox bound an other method displaying the state as text. My converter gets called, I know that for sure because I can place a break point in it.
So there is something wrong with my converter, because I allways ends up with a collapsed image.
**** EDIT ****
Here are some output from my debug.Writeline
I startup Connected:
value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed
I change to pending:
value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible
I startup pending:
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
This is correct because my program defaults to connected, and after a second it realizes that it can not see the TCP server but still have acces to Wifi so i changes state to pending.
回答1:
From your comments, it is most likely that your ConnectionWithServerEstablished
property does not change to make the images visible and/or you do not fire a PropertyChanged
event when the property value changed.
You can do this for example by firing the event in the setter of your dependent properties:
public bool IRCommandSent
{
set
{
// set the value
// ...
// notify event listeneers that the ConnectionWithServerEstablished may have changed
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ConnectionWithServerEstablished"));
}
}
}
The class you use as DataContext (your ViewModel) must of course implement INotifyPropertyChanged
for that.
来源:https://stackoverflow.com/questions/26924718/ivalueconverter-and-visibility