如何在xaml中绑定?

发布于 2024-11-09 10:05:11 字数 280 浏览 0 评论 0原文

现在我正在这样做绑定: 字段:

private readonly RestaurantContext m_context = new RestaurantContext();

init:

m_context.Load(m_context.GetGroupQuery());
this.dataGridGroup.DataContext = m_context.Groups;

在 xaml 中如何做到这一点?

now I'm doing so Binding:
field:

private readonly RestaurantContext m_context = new RestaurantContext();

init:

m_context.Load(m_context.GetGroupQuery());
this.dataGridGroup.DataContext = m_context.Groups;

How do this in xaml ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

差↓一点笑了 2024-11-16 10:05:11

只需公开您的 m_context,确保封装此属性的类设置为视图的数据上下文,并将您的 dataGridGroup 数据上下文绑定到您的属性。

例如:

public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
        DataContext = new WindowViewModel();//this will set the  WindowViewModel object below as  the datacontext of the window
    }
}

public class WindowViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }

    public WindowViewModel()
    {
        restContext = new RestaurantContext();//init 1
        restContext.Load(restContext.GetGroupQuery());//init 2
        InvokePropertyChanged(new PropertyChangedEventArgs("RestContext"));//notify the view th update datacontext
    }

    private RestaurantContext restContext;
    /// <summary>
    /// Gets or sets the RestContext (which will be vound to the datagrid datacontext)
    /// </summary>
    public RestaurantContext RestContext
    {
        get { return restContext; }
        set
        {
            if (RestContext != value)
            {
                restContext = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("RestContext"));
            }
        }
    }


}

/// <summary>
/// Whatever class
/// </summary>
public class RestaurantContext
{
    public void Load(object getGroupQuery)
    {
        //Whatever here 
    }

    public object GetGroupQuery()
    {
        //Whatever here 
        return new object();
    }

    IEnumerable Groups { get; set; } 
}

XAML:

 <Window x:Class="StackOverflow.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Width="100" Height="100" >
   <Grid>
      <DataGrid DataContex="{Binding RestContext.Groups}"></DataGrid>
   </Grid>
</Window>

Juste expose your m_context, ensure that the class that encapsulate this property is set as the datacontext of your view and bind your dataGridGroup datacontext to your prperty.

For example :

public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
        DataContext = new WindowViewModel();//this will set the  WindowViewModel object below as  the datacontext of the window
    }
}

public class WindowViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }

    public WindowViewModel()
    {
        restContext = new RestaurantContext();//init 1
        restContext.Load(restContext.GetGroupQuery());//init 2
        InvokePropertyChanged(new PropertyChangedEventArgs("RestContext"));//notify the view th update datacontext
    }

    private RestaurantContext restContext;
    /// <summary>
    /// Gets or sets the RestContext (which will be vound to the datagrid datacontext)
    /// </summary>
    public RestaurantContext RestContext
    {
        get { return restContext; }
        set
        {
            if (RestContext != value)
            {
                restContext = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("RestContext"));
            }
        }
    }


}

/// <summary>
/// Whatever class
/// </summary>
public class RestaurantContext
{
    public void Load(object getGroupQuery)
    {
        //Whatever here 
    }

    public object GetGroupQuery()
    {
        //Whatever here 
        return new object();
    }

    IEnumerable Groups { get; set; } 
}

XAML :

 <Window x:Class="StackOverflow.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Width="100" Height="100" >
   <Grid>
      <DataGrid DataContex="{Binding RestContext.Groups}"></DataGrid>
   </Grid>
</Window>
弱骨蛰伏 2024-11-16 10:05:11

在您的 XAML 中:

<DataGrid x:Name="dataGridGroup" DataContext={Binding Groups} />

它将自动绑定到您的 ViewModelGroups 属性

In your XAML:

<DataGrid x:Name="dataGridGroup" DataContext={Binding Groups} />

It will automatically bind to the Groups property of your ViewModel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文