14

我的代码在启动时失败,因为由Converter调用的 values 数组Multibinding填充正确的值,但值为 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();
        }
    }

您可以下载完整代码或查看我的代码片段,如下所示。

主窗口.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>

主窗口.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 类。

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");
            }
        }

    }
4

5 回答 5

10

当您在任何 ItemsSource 中设置列​​表时,DataTemplate 的 DataContext 将针对各个项目将列表中的每个项目。

确实,您的 TextBlock 绑定工作正常,因为 DataContext 设置为您的主对象:PageViewModel

但是在您的 DataTemplate 中, DataContext 将设置为PageNumberViewModel,因为这些是您集合中的项目。

因此,绑定到Path=CurrentPage.Page_Number将导致 UnsetValue,因为 CurrentPage 不是PageNumberViewModel

希望这能澄清事情!

如果您确实希望绑定到 Window 的 DataContext 的 CurrentPage 属性,请考虑使用 ElementName 绑定:

给窗口一个名字,绑定到

<Binding ElementName="name" Path="DataContext.CurrentPage.Page_Number" />

或使用 RelativeSource 绑定:

<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.CurrentPage.Page_Number" />
于 2011-07-22T08:21:17.883 回答
8

看起来您可能没有正确的 DataContext 来解析路径CurrentPage.Page_Number。调试此类事情的一个好方法是删除路径,以便您可以检查值转换器中的 DataContext:

<Binding Path="." />

然后在你的设置一个断点,ButtonColorConverter看看你想要转换的究竟是什么。

于 2011-07-22T07:24:25.087 回答
6

DependencyProperty.UnsetValue仅仅是 DependencyProperty 类上的一个常量。

你可以这样做:

 if (values[1] == DependencyProperty.UnsetValue)
 {
     return null;  // or default value
 }
于 2014-11-13T07:42:47.180 回答
5

我也遇到了这个问题,并在另一篇文章(https://stackoverflow.com/a/3139397/500099)中找到了解决方案。关键是像这样使用 FallbackValue="" 属性:

<MultiBinding Converter="{StaticResource StringFormatConverter}">
    <Binding Path="ResultValueControl.Min" FallbackValue=""/>
    <Binding Path="Format" />
</MultiBinding>
于 2012-12-19T15:12:37.687 回答
0

我刚刚根据Mr.Arcturus 的建议更改了 MainWindow.xaml 并且工作正常。

非常感谢大角星先生。

chage 后可以看到ManiWindow.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 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>
于 2011-07-22T08:38:34.830 回答