Git Product home page Git Product logo

Comments (4)

runceel avatar runceel commented on July 22, 2024

試してみて無いのですが、MethodTarget{Binding} になってるので ProductList の各行のインスタンスの HyperlinkProductAction が指定されているように感じます。

なので ViewModel のメソッドを指定したい場合は MthodTarget も ViewModel にしてあげる必要があります。いくつか書き方がありますが、一番簡単そうなのだとこんな感じでしょうか。

<!-- DataGrid に名前をつけて -->
<DataGrid x:Name="dataGrid" ....>
<!-- DataGrid の DataContext をメソッドのターゲットにする -->
<l:LivetCallMethodAction
  MethodName="HyperlinkProductAction" 
  MethodTarget="{Binding DataContext, ElementName=dataGrid}"/>

今すぐには時間がとれないのですが、こんな感じでどうでしょうか。ポイントは MethodTarget にちゃんとメソッドが定義されているオブジェクトを渡すというところになります。

from livet.

le-dn avatar le-dn commented on July 22, 2024

早速のご返答ありがとうございます。

ご指導のように書いてみたが、残念ながら、ViewModelにあるメソッドを呼べませんでした。

だが、下記のような書き方で、ViewModelにあるコマンドを呼べました。

<DataGrid ItemsSource="{Binding ProductModel.ProductList}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Product" Width="150" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Hyperlink 
                            Command="{Binding 
                                Path=DataContext.HyperlinkProductCommand, 
                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
                            <TextBlock Text="{Binding Path=ProductName}"/>
                        </Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

DataGridHyperlinkColumnにLivetCallMethodActionを使うことなく、DataGridTemplateColumnに普通のHyperlinkタグを作って、その中にCommandを書くことで、ViewModelにあるHyperlinkProductCommandを呼べました。

問題が解決されたかもしれないが、他の困っているところが出ます。
選択された行のProductIdをCommandParameterでViewModelに渡して欲しいが、渡す方法はまだ見つかりません。

<DataGrid ItemsSource="{Binding ProductModel.ProductList}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ProductId}" Width="50" IsReadOnly="True"/>
        <DataGridTemplateColumn Header="Product" Width="150" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Hyperlink 
                            Command="{Binding 
                                Path=DataContext.HyperlinkProductCommand, 
                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                            CommandParameter="{Binding Path=ProductId}"> <!-- これを渡したい! -->
                            <TextBlock Text="{Binding Path=ProductName}"/>
                        </Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

ViewModelにあるコマンドは渡されたProductIdを習得して欲しいです。

private ViewModelCommand _HyperlinkProductCommand;
public ViewModelCommand HyperlinkProductCommand
{
    get
    {
        if (_HyperlinkProductCommand == null)
        {
            _HyperlinkProductCommand = new ViewModelCommand(HyperlinkProduct);
        }
        return _HyperlinkProductCommand;
    }
}

public void HyperlinkProduct()
{
    // 渡されたProductIdを習得したい!
}

2回目の質問は申し訳ございません。
ご指導のほどよろしくお願いいたします。
m(_ _;)m

from livet.

runceel avatar runceel commented on July 22, 2024

ListenerCommand の型引数あり版だとどうですか?

from livet.

le-dn avatar le-dn commented on July 22, 2024

早速のご返答ありがとうございます。

ご指導の通り、ListenerCommandを使って、渡されたProductIdを習得できました。

private ListenerCommand<int> _HyperlinkProductCommand;
public ListenerCommand<int> HyperlinkProductCommand
{
    get
    {
        if (_HyperlinkProductCommand == null)
        {
            _HyperlinkProductCommand = new ListenerCommand<int>(HyperlinkProduct);
        }
        return _HyperlinkProductCommand;
    }
}

public void HyperlinkProduct(int productId)
{
    // 習得されたproductIdで操作する・・・
}

問題を解決できたので、このイシューを修了にします。

お忙しいところご指導いただき、本当にありがとうございます。
今後ともよろしくお願いいたします。
m(_ _;)m

from livet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.