6

一部の機能をクラス間で分割する必要があったため、次のような状況になりました。

xamlコード

<CheckBox IsChecked="{Binding MyObjectField.MyBoolean}"  />

モデルを見る

...
public MyInternalObject MyObjectField;
...

MyObjectクラス

public class MyInternalObject {
    ...
    public bool MyBoolean { get; set; }
    ...
}

ビューモデルクラスでMyBooleanプロパティを複製しない限り、機能しません。

public bool MyBoolean 
{ 
    get { return MyInternalObject.MyBoolean; }
    set { MyInternalObject.MyBoolean=value; }
}

誰かアイデアがありますか?

4

3 に答える 3

6

まだできません(WPFバージョン4.5では、静的プロパティにバインドできます)。ただし、App.xaml.csでプロパティを作成できます

public partial class App : Application
{
    public bool MyBoolean { get; set; }
}

どこからでもバインドできます。

<CheckBox IsChecked="{Binding MyBoolean, Source={x:Static Application.Current}}">
于 2012-07-24T16:39:57.270 に答える
5

いいえ、できません。バインディングシステムはReflectionを使用して

DataContextのプロパティ(つまりVM)

フィールドは検索しません。これがお役に立てば幸いです。

于 2012-07-24T16:26:11.207 に答える
0

要素をフィールドのプロパティにバインドする代わりに、要素のDataContextを必須フィールドに変更しました。

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MainWindowView mainWindowView = new MainWindowView();
        var mainWindowViewModel = new MainWindowViewModel();
        mainWindowView.DataContext = mainWindowViewModel;
        mainWindowView.pagerView.DataContext = mainWindowViewModel.pager;
        mainWindowView.Show();
    }

この例では、その下にDataGridとPager(最初、前、次、最後のページ)があります。MainWindowViewの要素(DataGridを含む)はMainWindowViewModelのプロパティにバインドされていますが、ページャーボタンはmainWindowViewModel.pagerのプロパティにバインドされています。

MainWindowView:

    <DataGrid Name="dgSimple" ItemsSource="{Binding DisplayedUsers}" MaxWidth="200" Grid.Row="0" SelectedItem="{Binding SelectedRow}"></DataGrid>
    <view:PagerView x:Name="pagerView" Grid.Row="2"/>

PagerView:

<UserControl x:Class="wpf_scroll.View.PagerView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:wpf_scroll.View"
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="350">
<StackPanel Orientation="Horizontal" Grid.Row="1">
    <Label Content="Page size:"/>
    <TextBox Text="{Binding PageSize}" Width="30" VerticalContentAlignment="Center"
                 HorizontalContentAlignment="Center"></TextBox>
    <Button Content="First" Command="{Binding FirstPageCommand}"></Button>
于 2018-03-13T17:47:41.610 に答える