C# IMultiValueConverter始终为列表传入DependencyProperty.UnsetValue
我有一个滑动控件,可以选择列表中的一个元素。我需要显示所选元素的ToString()。我遇到的问题是我的C# IMultiValueConverter始终为列表传入DependencyProperty.UnsetValue,c#,.net,wpf,.net-3.5,C#,.net,Wpf,.net 3.5,我有一个滑动控件,可以选择列表中的一个元素。我需要显示所选元素的ToString()。我遇到的问题是我的IMultiValueConverter中values的值。Convert函数始终为值[0]提供正确的值,但值[1]始终为dependencProperty.UnsetValue。我在装订方面做错了什么 这是我的XAML <Window x:Class="VetWebConnectorWPF.MainWindow" xmlns="http://schemas.micros
IMultiValueConverter中values
的值。Convert
函数始终为值[0]
提供正确的值,但值[1]
始终为dependencProperty.UnsetValue
。我在装订方面做错了什么
这是我的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>
我在您的XAML中没有看到任何名为
lstresourceions
是WPF的属性系统使用的而不是
null
来指示属性存在,但它没有值我在XAML中没有看到任何名为lstresourceions的元素
是WPF的属性系统使用的而不是
null
来指示属性存在,但它没有值lstresourceions是在codebehind文件readonly List lstresourceions
@ScottChamberlain中定义的,但UI在运行时无法找到ElementName
bindings通过可视化树查找具有特定名称的元素,在您的情况下,可视化树中没有名为lstResolutions
的元素,我需要做什么才能允许LstResourceIons被绑定?@ScottChamberlain一种方法是使用get方法将其创建为公共属性,然后更改绑定以指向该属性。这可能需要使用相对资源
绑定来查找窗口
对象,然后访问窗口
上的lstdesolutions
属性
LSTROUCELTIONS是在codebehind文件只读列表LSTROUCELTIONS
@ScottChamberlain中定义的,但用户界面在运行时无法找到ElementName
bindings通过可视化树查找具有特定名称的元素,在您的情况下,可视化树中没有名为lstResolutions
的元素,我需要做什么才能允许LstResourceIons被绑定?@ScottChamberlain一种方法是使用get方法将其创建为公共属性,然后更改绑定以指向该属性。这可能需要使用相对资源
绑定来查找窗口
对象,然后访问窗口
上的lstdesolutions
属性<代码>
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)
}
}
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();
}
}
}