簡體   English   中英

附屬物綁定不起作用

[英]Attached Property Binding not working

我創建了一個附加屬性來使用datagridRow上的inputbinding或元素樣式的datagricell來設置命令。

在這里我試圖在動態創建的東西上添加inputBinding,例如datagridrows / cells,這就是我試圖在樣式中添加inputbinding的原因。

不幸的是,樣式不允許我使用xaml中datagridrows / cells的InputBindings屬性,並且需要在代碼中設置這就是我需要附加屬性的原因。

附加屬性propertychangedcallback方法是在每個行/單元格創建時觸發,並且方法重新設置屬性設置的對象和屬性的值。 該值是輸入綁定集合。 在此集合中,項目存在但這些項目的屬性不與其值綁定

在xaml中你可以看到我只為所有行/單元創建了一個InputBinding,但是我需要在每一行上復制這個Inputbinding,因為它們需要自己的commandParameter,否則commandParameter對於每一行/單元都是相同的

當我刪除I​​nputBindingsCollection並使用單個InputBinding時,它可以工作,但我只能在inputbinding上設置,我想要一個鼠標綁定和一個鍵綁定,所以我需要使用InputBindingsCollection。

當我使用InputBindingsCollection時,集合的inputBindings沒有正確綁定

InputBindingsPropertyChangedCallBack方法中, inpuBind.CommandinpuBind.CommandParameter為null而不是綁定值

AttacheCommand

public class AttacheCommand
{
    public static DependencyProperty InputBindingsProperty = DependencyProperty.RegisterAttached("InputBindings", 
                                                                                                 typeof(InputBindingCollection),
                                                                                                 typeof(AttacheCommand),
                                                                                                 new PropertyMetadata(InputBindingsPropertyChangedCallBack));
    public static void SetInputBindings(UIElement element, InputBindingCollection value)
    {
        element.SetValue(InputBindingsProperty, value);
    }

    public static InputBindingCollection GetInputBindings(UIElement element)
    {
        return (InputBindingCollection)element.GetValue(InputBindingsProperty);
    }

    public static void InputBindingsPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = d as UIElement;
        InputBinding copy;
        foreach (InputBinding inpuBind in e.NewValue as InputBindingCollection)
        {
           //*****Here is my problem inpuBind.Command and inpuBind.CommandParameter are not bind to their value and are null
            copy = new InputBinding(inpuBind.Command, inpuBind.Gesture);
            copy.CommandParameter = inpuBind.CommandParameter;

            element.InputBindings.Add(copy);
        }
    }
}

視圖:

<Window x:Class="UltimateCommand.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:UltimateCommand"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:ViewModel x:Name="vm"/>
</Window.DataContext>
<Grid>
    <DataGrid ItemsSource="{Binding listModel1}" IsReadOnly="True">
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="local:AttacheCommand.InputBindings" >
                    <Setter.Value>
                        <InputBindingCollection>
                            <MouseBinding Command="{Binding ElementName=vm, Path=cmd1}" CommandParameter="{Binding}" Gesture="LeftClick"/>
                        </InputBindingCollection>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
</Grid>
</Window>


ViewModel :(和命令)

class ViewModel
{
    public Model1[] listModel1
    {
        get
        {
            return new Model1[] { new Model1("nom1", 1), new Model1("nom2", 2) };
        }
    }

    public Command1 cmd1
    {
        get
        {
            return new Command1();
        }
    }
}

public class Command1 : ICommand
{
    public bool CanExecute(object parameter)
    {
        //throw new NotImplementedException();
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        //throw new NotImplementedException();
    }
}


型號1:

public class Model1
{
    public string Nom { get; set; }
    public int Age { get; set; }

    public Model1(string n, int a)
    {
        Nom = n;
        Age = a;
    }
}

如果我正確地談論附加屬性或命令類必須是靜態的

所以你必須編寫公共靜態類AttachedCommand {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM