将DataGrid的CellTemplate内容绑定到模板化CustomControl上定义的Element或DependencyProperty [英] Binding a DataGrid's CellTemplate content to an Element or a DependencyProperty defined on the templated CustomControl

查看:84
本文介绍了将DataGrid的CellTemplate内容绑定到模板化CustomControl上定义的Element或DependencyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义控件中使用WPF的常规DataGrid。
DataGrid的单元格模板内容之一应绑定到文本块的文本或自定义控件上的依赖属性。 (如果我可以绑定到任何一个对我来说足够好)



我尝试使用ElementName执行以下绑定,但它没有工作。我继续得到一个DependencyProperty.UnsetValue -

 < DataGridTemplateColumn Header =Test> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< ContentPresenter>
< ContentPresenter.Content>
< MultiBinding Converter ={StaticResource TextToSpecialTextblockConverter}>
< Binding Path =SomeTextOnTheViewModel/>
< Binding ElementName =SearchBoxPath =TextMode =OneWay/>
< / MultiBinding>
< /ContentPresenter.Content>
< / ContentPresenter>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>

绑定到DependencyProperty也不起作用。

 < DataGridTemplateColumn Header =Test> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< ContentPresenter>
< ContentPresenter.Content>
< MultiBinding Converter ={StaticResource TextToSpecialTextblockConverter}>
< Binding Path =SomeTextOnTheViewModel/>
< Binding RelativeSource ={RelativeSource Mode = TemplatedParent}Path =SomeDP/>
< / MultiBinding>
< /ContentPresenter.Content>
< / ContentPresenter>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>

我希望有人可以帮助我!



谢谢!

解决方案

如果属性 Property DataContext 的控件或 DataGrid 中设置视图模型,则此示例的工作原理如下:



资源:

 < DataTemplate x:Key =template> 
< StackPanel Orientation =Horizo​​ntal>
< TextBlock Text =Test/>
< TextBlock Text ={Binding Path = DataContext.Property,RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}/>
< / StackPanel>
< / DataTemplate>

DataGrid

 < DataGrid ItemsSource ={Binding Items}...> 
< DataGrid.Columns>
< DataGridTemplateColumn CellTemplate ={StaticResource template}/>

DependencyProperty更改通知可以更新视图模型。



例如:

  public static readonly DependencyProperty TestProperty = 
DependencyProperty.Register(Test,typeof (string),typeof(DataGridComboBoxColumn),new PropertyMetadata(default(string),PropertyChangedCallback));

public string Test
{
get {return(string)GetValue(TestProperty); }
set {SetValue(TestProperty,value);


private static void PropertyChangedCallback(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((MyControl)dependencyObject).OnTestChanged();
}

private void OnTestChanged()
{
((MyViewModel)theGrid.DataContext).Property = Test;
}

为了绑定到依赖属性 / code>使用模板绑定使用此

 < DataTemplate x:Key =template2> 
< StackPanel Orientation =Horizo​​ntal>
< TextBlock Text =Test:/>
< TextBlock Text ={Binding Path = Test,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type local:CustomControl1}}}/>
< / StackPanel>
< / DataTemplate>

< Style TargetType ={x:Type local:CustomControl1}>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type local:CustomControl1}>
< Border Background ={TemplateBinding Background}
BorderBrush ={TemplateBinding BorderBrush}
BorderThickness ={TemplateBinding BorderThickness}>
< DataGrid ItemsSource ={TemplateBinding ItemsSource}AutoGenerateColumns =True>
< DataGrid.Columns>
< DataGridTemplateColumn Header =TestCellTemplate ={StaticResource template2}/>
< /DataGrid.Columns>
< / DataGrid>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>

其中CustomControl1是

  public class CustomControl1:ItemsControl 
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(CustomControl1),
新的FrameworkPropertyMetadata(typeof(CustomControl1)));


public static readonly DependencyProperty TestProperty =
DependencyProperty.Register(Test,typeof(string),typeof(CustomControl1),new PropertyMetadata(default(string))) ;

public string Test
{
get {return(string)GetValue(TestProperty); }
set {SetValue(TestProperty,value); }
}
}


I'm using WPF's regular DataGrid inside a custom control. One of the DataGrid's cell template content should be bound to a the Text of a Textblock or to a DependencyProperty on the custom control. (If I could bind it to any of them it's good enough for me)

I tried to do the following binding with the ElementName but it didn't work. I keep getting an DependencyProperty.UnsetValue -

<DataGridTemplateColumn Header="Test">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <ContentPresenter>
                <ContentPresenter.Content>
                    <MultiBinding Converter="{StaticResource TextToSpecialTextblockConverter}">
                       <Binding Path="SomeTextOnTheViewModel"/>
                       <Binding ElementName="SearchBox" Path="Text" Mode="OneWay"/>
                    </MultiBinding>
                </ContentPresenter.Content>
             </ContentPresenter>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Binding to a DependencyProperty also doesn't work.

<DataGridTemplateColumn Header="Test">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <ContentPresenter>
                <ContentPresenter.Content>
                    <MultiBinding Converter="{StaticResource TextToSpecialTextblockConverter}">
                       <Binding Path="SomeTextOnTheViewModel"/>
                       <Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="SomeDP" />
                    </MultiBinding>
                </ContentPresenter.Content>
             </ContentPresenter>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I hope someone could help me out!

Thanks!

解决方案

If the property Property is defined on a view model set on the DataContext of the control or DataGrid then this sample works:

Resources:

    <DataTemplate x:Key="template">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Test"/>
            <TextBlock Text="{Binding Path=DataContext.Property,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
        </StackPanel>
    </DataTemplate>

DataGrid

<DataGrid ItemsSource="{Binding Items}" ...>
    <DataGrid.Columns>
        <DataGridTemplateColumn CellTemplate="{StaticResource template}" />

The DependencyProperty change notification could update the view model.

For example:

    public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register("Test", typeof (string), typeof (DataGridComboBoxColumn), new PropertyMetadata(default(string), PropertyChangedCallback));

    public string Test
    {
        get { return (string) GetValue(TestProperty); }
        set { SetValue(TestProperty, value); }
    }

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        ((MyControl) dependencyObject).OnTestChanged();
    }

    private void OnTestChanged()
    {
        ((MyViewModel) theGrid.DataContext).Property = Test;
    }

In order to bind to a dependency property Test using a template binding use this

<DataTemplate x:Key="template2">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Test: "/>
        <TextBlock Text="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}}}"/>
    </StackPanel>
</DataTemplate>

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <DataGrid ItemsSource="{TemplateBinding ItemsSource}" AutoGenerateColumns="True">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="Test" CellTemplate="{StaticResource template2}" />
                        </DataGrid.Columns>
                    </DataGrid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Where CustomControl1 is

public class CustomControl1 : ItemsControl
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof (CustomControl1),
            new FrameworkPropertyMetadata(typeof (CustomControl1)));
    }

    public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register("Test", typeof (string), typeof (CustomControl1), new PropertyMetadata(default(string)));

    public string Test
    {
        get { return (string) GetValue(TestProperty); }
        set { SetValue(TestProperty, value); }
    }
}

这篇关于将DataGrid的CellTemplate内容绑定到模板化CustomControl上定义的Element或DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆