开发者

How can I pass data back from my XAML to my binding source?

开发者 https://www.devze.com 2023-01-04 01:49 出处:网络
I\'ve XAML that looks a bit like this: <UserControl x:Class=\"MyNamespace.MyClass\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"

I've XAML that looks a bit like this:

<UserControl x:Class="MyNamespace.MyClass"
         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:MyNamespace"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="500">

<UserControl.Resources>
    <local:MyViewModel x:Key="ViewModel"/>
</UserControl.Resources>

<UserControl.DataContext>
    <Binding Source="{StaticResource ViewModel}"/>
</UserControl.DataContext>

    <DataGrid Name="_myGrid" AutoGenerateColumns="False" 
   ItemsSource="{Binding Path=MyDataTable}">        
   <DataGrid.Columns>
            <DataGridTemplateColumn Header="Name">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=name}" />
                            <TextBlock Text="{Binding 
                                Source={StaticResource ViewModel}, Path=IsValidName}"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    &开发者_开发知识库lt;DataTemplate>
                        <Grid FocusManager.FocusedElement="{Binding ElementName=NameTextBox}">
                            <TextBox Name="NameTextBox" Text="{Binding Path=name}" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

As you can probably see above, my DataGrid binds to a data table as its ItemsSource. I typically bind to columns in the data table view, such as 'name'. In some cases, I bind to my view model, rather than a DataRowView. That can be seen in the 2nd TextBlock above, where I bind to the view model's IsValidName property. My question is, how can I pass information from my XAML back to my binding source, in this case my view model? I'd like to pass the rowIndex, DataRowView object, name, or something similar so that the IsValidName property on the view model would have context as to which row is being validated.

Thanks,

Notre


Looks like you want to call your IsValidName Property like a Method passing Parameters as to which row is being validated.

if you have control over each row's Type, implement the IsValidName Property there, completely avoiding the StaticResource Binding!

0

精彩评论

暂无评论...
验证码 换一张
取 消