【问题标题】:Can I bind a WPF control to a field's property?我可以将 WPF 控件绑定到字段的属性吗?
【发布时间】:2012-07-23 00:38:43
【问题描述】:

因为我需要在类之间拆分一些功能,所以我遇到了以下情况

xml 代码

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

查看模型

...
public MyInternalObject MyObjectField;
...

MyObject 类

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

除非我在 View Model 类中复制 MyBoolean 属性,否则它不起作用。

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

有人有想法吗?

【问题讨论】:

    标签: c# wpf binding wpf-controls


    【解决方案1】:

    不,你不能。因为绑定系统使用反射来查找

    DataContext 中的属性(即你的虚拟机)

    它不寻找字段。我希望这会有所帮助。

    【讨论】:

    • 所以复制虚拟机中的属性是唯一的方法......感谢您的帮助
    • 是的,并且在属性的设置器中设置值后,还为“MyBoolean”引发 PropertyChanged。
    • 是的,我正在简化示例,但无论如何感谢指出。
    【解决方案2】:

    您还不能 (in WPF Version 4.5 you can bind to a static property)。但是您可以在 App.xaml.cs 中创建您的属性

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

    并从任何地方绑定。

    <CheckBox IsChecked="{Binding MyBoolean, Source={x:Static Application.Current}}">
    

    【讨论】:

    • 谢谢。也很高兴知道这个选项。
    【解决方案3】:

    我没有将元素绑定到字段的属性,而是将元素的 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 的属性。

    主窗口视图:

        <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多