ItemsControl数据绑定不起作用?

时间:2015-07-27 10:34:41

标签: c# .net wpf

我有这个WPF结构:

<UserControl 
      xmlns:viewModel="clr-namespace:..ViewModel.ToneAudiogramLegend">
      ...
  <DataTemplate DataType="{x:Type viewModel:ToneAudiogramLegendTableViewModel}">
      ...

    <DataGrid  Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ToneAudiogramLegneds}" HeadersVisibility="None" AutoGenerateColumns="False" IsReadOnly="True" BorderBrush="Transparent" BorderThickness="0"
                                   MinWidth="100" Height="{Binding Height, Mode=OneWay}" KeyboardNavigation.DirectionalNavigation="None" Grid.ColumnSpan="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" GridLinesVisibility="None" ColumnWidth="*"
                                  Margin="1" wpfmvvm:DataGridRowHeightBehaviour.AutoFitRowHeight="True" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn HeaderTemplate="{x:Null}">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Label x:Name="PART_Content" Content="{Binding Path=Left.Content}" 
                                            HorizontalAlignment="Center" VerticalAlignment="Center"
                                            Foreground="Gold"/>

                                            <DataTemplate.Triggers>
                                                  <DataTrigger Binding="{Binding Path=IsMonochrome}" Value="True">
                                                   <Setter TargetName="PART_Content" Property="Foreground" Value="Green"/>
                                                  </DataTrigger>
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
  ...
  <DataTemplate>

Value的绑定有效,因为颜色的默认值为Left.Color的{​​{1}}

<Setter Property="Foreground" Value="{Binding Path=Left.Color}"/>属性会在发生特定事件时更改值,但前景色不会更改为绿色。我不太确定格式和结构是否合适。我不确定IsMonochrome是否有问题,因为我有另一个<DataTemplate>更高的顺序

课程:

<DataTemplate>

public partial class ToneAudiogramLegendTableViewModel : ViewModelBase, IToneAudiogramLegendTableViewModel, IHandleMonochromeReportElement
{
    public bool IsMonochrome
            {
                get { return GetValue<bool>("IsMonochrome"); }
                private set { SetValue("IsMonochrome", value); }
            }

            public void SwitchToMonochromeMode()
            {
                IsMonochrome = true;
            }

            public void SwitchToColorMode()
            {
                IsMonochrome = false;
            }
}

public class ToneAudiogramLegendViewModel : ViewModelBase, IToneAudiogramLegendVM
    {
        public string Name
        {
            get { return GetValue<string>("Name"); }
            set { SetValue("Name", value); }
        }

        public LegendViewModel Left
        {
            get { return GetValue<LegendViewModel>("Left"); }
            set { SetValue("Left", value); }
        }
}

可能是什么问题?

1 个答案:

答案 0 :(得分:2)

在您的CellTemplate中,您绑定到ToneAudiogramLegendViewModel对象,其中包含属性NameLeftIsMonochrome位于不同的对象上,这就是它不起作用的原因。

您需要定义IsMonochrome属性,或者您需要在ToneAudiogramLegendViewModel中引用ToneAudiogramLegendTableViewModel来创建属性,以便能够数据绑定到IsMonochrome

修改 根据您的意见:

<DataTrigger Binding="{Binding Path=DataContext.IsMonochrome, 
                               RelativeSource={RelativeSource DataGrid}}" 
             Value="True">