Scott Chamberlain
Scott Chamberlain

Reputation: 127603

IMultiValueConverter always passes in DependencyProperty.UnsetValue for list

I have a slider control that will select a element in a list. I need the ToString() of the selected element to be displayed. The issue I am having is the value of values in my IMultiValueConverter.Convert function always has a correct value for values[0] but values[1] is always DependencyProperty.UnsetValue. What am I doing wrong in my binding?

Here is my XAML

<Window x:Class="VetWebConnectorWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:VetWebConnectorWPF"
        <!--Snip-->
        >
    <Grid>
    <!--Snip-->
        <GroupBox Grid.Row="1" Height="Auto" Header="Display configuration">
            <Grid>
                <!--Snip-->
                <Grid Grid.Row="1" Width="200">
                    <!--Snip-->
                    <Slider Name="sldrResoultion" Grid.Column="1" TickPlacement="BottomRight" Minimum="0" Maximum="{Binding ElementName=lstResoultions, Path=Count}" TickFrequency="1"
                                        IsSnapToTickEnabled="True"/>
                </Grid>
                <Label Name="lblResoultionDisplay" Grid.Row="2" HorizontalAlignment="Center">
                    <Label.Resources>
                        <local:ResoutionConverter x:Key="resoutionConverter" />
                    </Label.Resources>
                    <Label.Content>
                        <MultiBinding Converter="{StaticResource resoutionConverter}">
                            <Binding ElementName="sldrResoultion" Path="Value" />
                            <Binding ElementName="lstResoultions" />
                        </MultiBinding>
                    </Label.Content>
                </Label>
            </Grid>
        </GroupBox>
    </Grid>
</Window>

Here is my codebehind for MainWindow

namespace VetWebConnectorWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.lstResoultions = new List<object>();

            AddResoution(new Resoultion(1024, 768));
            AddResoution(new Resoultion(1366, 768));
            AddResoution(new Resoultion(1280, 960));
            AddResoution(new Resoultion(1440, 900));
            AddResoution(new Resoultion(1280, 1024));
            AddResoution(new Resoultion(1600, 900));
            AddResoution(new Resoultion(1400, 1050));
            AddResoution(new Resoultion(1440, 1080));
            AddResoution(new Resoultion(1600, 1200));
            AddResoution(new Resoultion(1920, 1080));
            AddResoution(new Resoultion(1920, 1200));
            AddResoution(new Resoultion(2048, 1152));
            AddResoution(new Resoultion(2560, 2048));
            AddResoution(new Resoultion(3200, 2048));

            this.lstResoultions.Add("Full Screen");

        }

        readonly List<object> lstResoultions;

        //(Snip)
    }
}

Here is the code for Resoultion.cs

namespace VetWebConnectorWPF
{
    public class Resoultion
    {
        public Resoultion(int width, int height)
        {
            this.Width = width;
            this.Height = height;
        }

        public int Width { get; private set; }
        public int Height { get; private set; }

        public override string ToString()
        {
            return string.Concat(this.Width, " x ", this.Height);
        }
    }

    class ResoutionConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null || values.Length != 2)
                return null;

            double? idx = values[0] as double?;
            object[] resoultions = values[1] as object[];

            if (!idx.HasValue || resoultions == null)
                return null;

            return resoultions[System.Convert.ToInt32(idx.Value)];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Upvotes: 0

Views: 2479

Answers (1)

Rachel
Rachel

Reputation: 132618

I don't see any elements in your XAML named lstResoultions

DependencyProperty.UnsetValue is what WPF's property system uses instead of null to indicate that the property exists, but it does not have a value

Upvotes: 3

Related Questions