我的代码在启动时失败,因为Multibinding
调用的Converter
中的值数组未填充适当的值,但具有 DependencyProperty.UnsetValue
的值。
看一下转换器,看看我在哪里得到错误
public class ButtonColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string val1 = string.Format(" {0} ", values[0]);
string val2 = (string)values[1]; **//Here i am getting ==> {DependencyProperty.UnsetValue}**
return val1.Equals(val2)
? Brushes.NavajoWhite
: Brushes.White;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
你可以下载完整的代码或看到我的代码片段如下。
MainWindow.xaml
<Window x:Class="DataPager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:Local="clr-namespace:DataPager.Convertor_For_BackGround">
<Grid>
<Grid.Resources>
<Local:ButtonColorConverter x:Key="currentPageSetter"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="36*" />
<RowDefinition Height="275*" />
</Grid.RowDefinitions>
<ItemsControl Name="pageControl" ItemsSource="{Binding Path=PageCollection}" Grid.Row="0">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Border >
<StackPanel>
<ItemsPresenter></ItemsPresenter>
</StackPanel>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel x:Uid="pageItemTemplate">
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="pageNumberButton" Margin="3,4" Content="{Binding Path=Page_Number}">
<Button.Background>
<MultiBinding Converter="{StaticResource currentPageSetter}">
<Binding Path="Page_Number" />
<Binding Path="CurrentPage.Page_Number" /> **//This Binding not resolves properly**
</MultiBinding>
</Button.Background>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBox Text="{Binding Path=CurrentPage.Page_Number,Mode=TwoWay, FallbackValue=asdf}" Grid.Row="1" Height="23" Margin="79,62,257,0" Name="textBox1" VerticalAlignment="Top" Width="167" />
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
MyPageViewModel = new PageViewModel();
MyPageViewModel.PageCollection.Add(new PageNumberViewModel(string.Format(" {0} ",0)));
MyPageViewModel.PageCollection.Add(new PageNumberViewModel(string.Format(" {0} ",1)));
MyPageViewModel.PageCollection.Add(new PageNumberViewModel(string.Format(" {0} ",2)));
MyPageViewModel.PageCollection.Add(new PageNumberViewModel(string.Format(" {0} ",3)));
InitializeComponent();
}
public PageViewModel MyPageViewModel
{
get
{
return this.DataContext as PageViewModel;
}
set
{
this.DataContext = value;
}
}
}
这是ViewModel Classes
PageViewModel.cs
public class PageViewModel:ViewModelBase
{
private ObservableCollection<PageNumberViewModel> m_pageCollection = new ObservableCollection<PageNumberViewModel>();
private PageNumberViewModel m_currentPage = new PageNumberViewModel(string.Format(" {0} ",0));
public PageViewModel()
{
m_currentPage = new PageNumberViewModel(string.Format(" {0} ", 1000));
}
public PageNumberViewModel CurrentPage
{
get
{
return this.m_currentPage;
}
set
{
if (m_currentPage == value)
return;
this.m_currentPage = value;
base.OnPropertyChanged("CurrentPage");
}
}
public ObservableCollection<PageNumberViewModel> PageCollection
{
get
{
return this.m_pageCollection;
}
set
{
if (m_pageCollection == value)
return;
this.m_pageCollection = value;
base.OnPropertyChanged("PageCollection");
}
}
}
PageNumberViewModel.cs
public class PageNumberViewModel : ViewModelBase
{
private string m_pageNumber;
public PageNumberViewModel()
{
}
public PageNumberViewModel(string pageNumgerArg)
{
this.m_pageNumber = pageNumgerArg;
}
public string Page_Number
{
get { return m_pageNumber; }
set
{
if (m_pageNumber == value)
return;
m_pageNumber = value;
OnPropertyChanged("PageNumber");
}
}
}
当您在任何ItemsSource中设置列表时,单个项目的DataTemplate的DataContext将为列表中的每个项目设置。
你的TextBlock绑定工作正确,因为DataContext被设置为你的主要对象:PageViewModel
但是在您的DataTemplate中,DataContext将被设置为PageNumberViewModel
,因为这些是您的集合中的项目。
因此,与Path=CurrentPage.Page_Number
的绑定将导致UnsetValue,因为CurrentPage不是PageNumberViewModel
的属性
希望这能澄清一些事情!
如果你真的想绑定Window的DataContext的CurrentPage属性,考虑使用ElementName绑定:
给window一个名字,绑定到
<Binding ElementName="name" Path="DataContext.CurrentPage.Page_Number" />
或使用RelativeSource绑定:
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.CurrentPage.Page_Number" />
看起来您可能没有正确的DataContext来解析路径CurrentPage.Page_Number
。调试这类事情的一个好方法是删除路径,以便您可以检查值转换器中的DataContext:
<Binding Path="." />
然后在你的ButtonColorConverter
中设置一个断点,看看你到底想转换什么
DependencyProperty.UnsetValue
仅仅是DependencyProperty类中的一个常量。
你可以这样做:
if (values[1] == DependencyProperty.UnsetValue)
{
return null; // or default value
}
我也遇到了这个问题,并在另一个帖子(https://stackoverflow.com/a/3139397/500099)中找到了解决方案。关键是像这样使用FallbackValue="属性:
<MultiBinding Converter="{StaticResource StringFormatConverter}">
<Binding Path="ResultValueControl.Min" FallbackValue=""/>
<Binding Path="Format" />
</MultiBinding>
我刚刚更改了主窗口。xaml按照Mr。Arcturus的建议和它的工作良好。
非常感谢Mr.Arcturus
可以看到ManiWindow。xaml after change
<Window x:Class="DataPager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:Local="clr-namespace:DataPager.Convertor_For_BackGround">
<Grid>
<Grid.Resources>
<Local:ButtonColorConverter x:Key="currentPageSetter"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="36*" />
<RowDefinition Height="275*" />
</Grid.RowDefinitions>
<ItemsControl Name="pageControl" ItemsSource="{Binding Path=PageCollection}" Grid.Row="0">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Border >
<StackPanel>
<ItemsPresenter></ItemsPresenter>
</StackPanel>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel x:Uid="pageItemTemplate">
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="pageNumberButton" Margin="3,4" Content="{Binding Path=Page_Number}">
<Button.Background>
<MultiBinding Converter="{StaticResource currentPageSetter}">
<Binding Path="Page_Number" />
**<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.CurrentPage.Page_Number" />**
</MultiBinding>
</Button.Background>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBox Text="{Binding Path=CurrentPage.Page_Number,Mode=TwoWay, FallbackValue=asdf}" Grid.Row="1" Height="23" Margin="79,62,257,0" Name="textBox1" VerticalAlignment="Top" Width="167" />
<Button Content="Button" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="121,110,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>