Comments (4)
試してみて無いのですが、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.
早速のご返答ありがとうございます。
ご指導のように書いてみたが、残念ながら、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.
ListenerCommand
の型引数あり版だとどうですか?
from livet.
早速のご返答ありがとうございます。
ご指導の通り、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)
- nugetで2.1.0をインストールするときのエラー HOT 9
- 新しい Behavior ライブラリへの更新 HOT 7
- don't have English version readme HOT 2
- DispatcherCollection.Dispatcherはnull許容ですか? HOT 1
- README.mdの画像リンク切れ HOT 3
- Nuget ライブラリの更新 HOT 2
- ViewModelA->ViewModelBのメッセージ通知 HOT 9
- Latest version Livet throws error on WPF. HOT 3
- .NET Framework のプロジェクトテンプレートが空のプロジェクトを生成する
- VisualStudio2019にて新規プロジェクトでLivetテンプレートを使用するとClickOnce発行ができません。 HOT 4
- CursorTypeAndBooleanConverter is not working HOT 4
- GitHub Actions 上での単体テストが常に失敗する
- FolderSelectionMessage cause FileNotFoundException in ver 3.2.3. HOT 4
- FolderSelectionMessageでの複数選択 HOT 3
- Working for .NET 6 and Visual Studio 2022
- FolderSelectionMessage cause InvalidOperationException in ver 3.2.3.2 HOT 4
- .NET 6 で FolderBrowserDialog を開こうとするとエラー HOT 3
- FolderSelectionMessage.Response の型 HOT 2
- 本家DataTriggerが初期値に対応しました
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from livet.