【问题标题】:DatagridTemplateColumn with triggers failed to bind带触发器的 DatagridTemplateColumn 绑定失败
【发布时间】:2012-09-18 03:20:02
【问题描述】:

我有一个包含 2 列的数据网格,我想将第 2 列中的一些单元格显示为:

  1. 组合框
  2. 文本框

基于属性

代码:

解决方案 #1:

 <Window.Resources> 
<DataTemplate x:Key="DropDownTemplate">
     <StackPanel>

      <ComboBox  SelectedValuePath="Id" DisplayMemberPath="Name"  ItemsSource="{Binding MarketConfigurationLOVs, UpdateSourceTrigger=PropertyChanged}"/>

       </StackPanel>
</DataTemplate>

<DataTemplate x:Key="TextBoxTemplate">
      <StackPanel>
           <TextBox Text="{Binding ConfigurationValue, UpdateSourceTrigger=PropertyChanged}" />


       </StackPanel>
  </DataTemplate>
 </Window.Resources>

这里是数据网格标签:

<DataGrid ItemsSource="{Binding Path= MarketConfigurationValues,Mode=TwoWay}" HeadersVisibility="None" AutoGenerateColumns="False" CanUserAddRows="False">
     <DataGrid.Columns>
             <DataGridTextColumn Width="120" Binding="{Binding  Path= ConfigurationName}" />


             <DataGridTemplateColumn>
                   <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                              <ContentControl Content="{Binding}" >
                                     <ContentControl.Style>
                                          <Style TargetType="ContentControl">
                                               <Style.Triggers>

                                                   <DataTrigger Binding="{Binding HasLOV}" Value="true">
                                                         <Setter  Property="ContentTemplate"  Value="{StaticResource DropDownTemplate}" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding HasLOV}" Value="false">
                                                         <Setter  Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
                                                            </DataTrigger>
                                                            <!-- and so on -->
                                                        </Style.Triggers>
                                                    </Style>
                                                </ContentControl.Style>
                                            </ContentControl>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

   </DataGrid.Columns>

这未能显示实际值,因为它显示属性的名称并始终在下拉列表中显示单元格,当我将 TextBoxContentTemplate 与 ComboBoxContentTemplate 交换时,它会将所有单元格显示为文本框,因此它似乎忽略了触发器但是当我调试时我发现HasLOV 有些项目包含真有些包含假

解决方案 #2: (同样失败) 参考:Jon 解决方案来自以下帖子 WPF MVVM Creating Dynamic controls

<DataGridTemplateColumn>
  <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
       <ContentControl x:Name="MyContentControl" Content="{Binding}" />

         <DataTemplate.Triggers>
              <DataTrigger Binding="{Binding HasLOV}" Value="false">
                    <Setter  TargetName="MyContentControl" Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
              </DataTrigger>

              <DataTrigger Binding="{Binding HasLOV}" Value="true">
                    <Setter  TargetName="MyContentControl"  Property="ContentTemplate" Value="{StaticResource DropDownTemplate}" />
              </DataTrigger>

         </DataTemplate.Triggers>

      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

ViewModel 是大类,但我把与我的问题相关的主要部分拉到了这里。在构造函数中,我创建了 MarketConfigurationValue 列表,并在 MarketConfigurationLOVs 中填充了值,以防我设置 HasLov=true 并在 configurationValue 中填充值,以防我设置 HasLov=false 并运行应用程序并在调试模式下找到主对象列表包含我正确地说的内容,但在 HasLov=true 的情况下无法显示组合框,在 HasLov=false 的情况下无法显示 textBox :

public class MarketConfigurationValue
    {
        public string ConfigurationName { get; set; }
        public string ConfigurationValue { get; set; }
        public int ConfigurationValueId { get; set; }
        public List<Market_Config_Lov> MarketConfigurationLOVs {get;set;}
        public bool HasLov { get; set; }

    }

public class Market_Config_Lov 
    {
        public virtual string Name { get; set; }
        public virtual int Sequence { get; set; }
    }

你能帮忙吗?

【问题讨论】:

  • 尝试在 ContentControl 中通过 DataContext={Binding} 替换 Content={Binding}。
  • 尝试将触发器放在 DataTemplate.Triggers 下。也许这可以帮助:codeproject.com/Articles/18303/…
  • @Florian GI:当我将 Content={Binding} 替换为 DataContext={Binding} 为: 文本框始终为空且没有下拉列表
  • 我认为查看您的 ViewModel 会有所帮助。
  • @Florian GI :我更新了帖子以包含 ViewModel 你现在可以查看它

标签: wpf xaml


【解决方案1】:

以下是实现 INotifyPropertyChanged 接口的方法。这样,当调用 HasLOV 的设置器时,绑定会自行更新。

public class MarketConfigurationValue:INotifyPropertyChanged
{
    private bool _hasLOV;
    public bool HasLOV 
    { 
        get {return _hasLOV;} 
        set {_hasLOV = value; OnPropertyChanged("HasLOV");}
    }  
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        VerifyPropertyName(propertyName);

        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

希望这会有所帮助。

====>属性名称是拼写错误,因为类中的属性称为:HasLov,在 XAML 中称为 HasLOV。

【讨论】:

  • 我把这部分放在一个独立的项目中,我希望你检查一下。我可以附在此处或直接发送给您吗?
  • 你可以发到我的邮箱 ;)
  • 我找不到您的电子邮件。请发邮件到我的邮箱 thabet084@hotmail.com 我会回复附件
  • 是的,你不能隐藏它,或者我不知道我从哪里得到用户的电子邮件联系方式
  • 修复了我的问题,因为问题是拼写错误,属性名称是 HasLov,我在 xaml HasLOV 中使用了拼写错误的属性。感谢弗洛里安的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 2017-01-16
  • 1970-01-01
  • 2021-07-29
  • 2011-04-06
相关资源
最近更新 更多