vb.net - WPF の ControlTemplate の要素に異なる DataContext を設定する

okwaves2024-01-25  11

これ以上先に進めません。

私の構造は次のようになります:

ViewModel (Class)
   Property Number (Class, iValue)
                Value, InputValidationInfo, etc.
   Property Icon (Class, iValue)
                Value, InputValidationInfo, etc.

ページの DataContext は ViewModel です。ページ上に入力フィールドがあります。これらは、iValue (数値、アイコン) の 1 つに割り当てられます。すべての入力フィールドには ContentTemplate があります。 ContentTemplate には、iValue クラスのプロパティへのバインディングを持ついくつかの要素が含まれています。

<ControlTemplate TargetType="{x:Type Button}">
    <Border x:Name="border" Background="{TemplateBinding Background}">
        <DockPanel>
            <Border Width="10" Background="{Binding InputValidation, Converter={StaticResource InputValidationToBrushConverter}}" Margin="0">
                <Border.ToolTip>
                    <ToolTip Visibility="{Binding InputValidationInfoCount, Converter={StaticResource CountToVisibleConverter}}" >
                                        <ItemsControl ItemsSource="{Binding InputValidationInfo}"/>
                         </ToolTip>
                     </Border.ToolTip>
                 </Border>
                 <Border x:Name="ContentBorder" Margin="1">
                 <Grid>
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="*"/>
                         <ColumnDefinition Width="Auto"/>
                     </Grid.ColumnDefinitions>
                     <ContentPresenter Margin="5 1 0 1" x:Name="myButtonContent" HorizontalAlignment="Left" VerticalAlignment="Center" TextElement.Foreground="{StaticResource ButtonForegroundBrush}"/>
                     <StackPanel x:Name="Buttons" Grid.Column="1" Orientation="Horizontal" Visibility="Collapsed">
                         <Button x:Name="ResetButton" Style="{StaticResource styleInputBoxSubButton}" Content="{StaticResource symbolCancel}" FontFamily="{StaticResource symbolCancelFF}" Command="{Binding ResetValueCommand}"/>
                     </StackPanel>
                 </Grid>
             </Border>
         </DockPanel>
    </Border>
</ControlTemplate>

これは次のようになります:

<Button x:Name="btnIcon" Grid.Column="1" Grid.Row="2" DataContext="{Binding Icon}"
    Style="{StaticResource styleInputButton}" MinHeight="37"
    FontFamily="{Binding Value, Converter={StaticResource NumberToUserIconFontFamilyConverter}}"
    Content="{Binding Value, Converter={StaticResource NumberToUserIconConverter}}"
    Click="IconButton_Click"/>

私の問題はクリックイベントです。小さな [X] ボタンは、ICommand を使用して iValue クラスにバインドされています。これはうまくいきます。これは常に同じ iValue プロパティ (ResetValueCommand) であるため、iCommand を ControlTemplate のバインディングとして設定できます。

しかし、「休息」をクリックすると、実際のボタンは ViewModel のメソッドを呼び出す必要があります。あこのメソッドの名前は常に同じであるとは限りません。 ContentTemplate では設定できません。

DataContext がすでに iValue であるため、コマンドを ViewModel にバインドできません。 Clickイベントを使ってみました。ただし、このイベントは [X] ボタンをクリックしてもトリガーされます。

このボタンは頻繁に使用する必要があります。洗練されたソリューションが必要です。最も簡単なのは、page-XAML の ContentTemplate で特定の要素のデータ コンテキストとバインディングを指定できればよいということです。または、page-XAML の ContentTemplate で要素のクリック イベントを指定します。

これに基づいて、おそらくボタン用に別のビューモデルが必要だと思いますか?そうすれば、ボタンはフォームとは異なる独自のコンテキストを持つことができます。

– クレイグ

2020 年 9 月 3 日 13:11



------------------------

DataContext がすでに iValue であるため、コマンドを ViewModel にバインドできません ...

DataContext に関係なく、バインディングのソースを指定できます。

<Button x:Name="btnIcon" ...
        Command="{Binding DataContext.CommandPropertyOfViewModel, 
            RelativeSource={RelativeSource AncestorType=Page}}"/>

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。