绑定到以编程方式设置的DataContext

时间:2013-08-31 11:15:31

标签: c# wpf xaml mvvm

我正在尝试将SIDJavnaUstanovaViewModel的Property IsWriteAllowed设置为TextBox。 我不能这样做,我尝试了相对来源,ElementName,但它只是不起作用。 我试图将IsWriteAllowed属性设置为TextBox IsEnabled属性。 在XAML-s中我有StaticResource,这很好用,但是在这里,当我设置DataContext programmaticaly时,我无法绑定它。 我以编程方式设置了DataContext:

SIDJavnaUstanovaViewModel definitionvm = new SIDJavnaUstanovaViewModel();
definitionvm.FillElements(null);
Definition definition = new Definition();   // Create new XAML 
definition.DataContext = definitionvm;      // Set its DataContext
definition.Show();

XAML看起来像这样:

<src:BaseWindow x:Class="StoreIDCard.Definition"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"   // i tried without that, but nothing happens 
xmlns:src="clr-namespace:StoreIDCard.Base"
xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:viewmod="clr-namespace:StoreIDCard.ViewModel"
x:Name="DefinitionWin"
WindowStartupLocation="CenterScreen"
ResizeMode="CanMinimize" ShowInTaskbar="True"   
xmlns:my="clr-namespace:StoreIDCard.View" Width="521" Height="494" 
Icon="/StoreIDCard;component/Images/Delhaize.png" IsEnabled="{Binding}">
<Grid Height="465" Width="503" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="476*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button Content="{StaticResource save}" Command="{Binding SaveAndUpdate}" Height="34" HorizontalAlignment="Left" Margin="188,411,0,0" VerticalAlignment="Top" Width="133" />

    <DataGrid AutoGenerateColumns="False" Block.TextAlignment="Left"  Height="331"   Visibility="{Binding Path=IsVisible}"   ItemsSource="{Binding Path=Elements}" Name="dataGrid2" SelectedItem="{Binding  SelectedElement}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="371" UseLayoutRounding="True" VerticalScrollBarVisibility="Auto" Margin="73,53,0,0">
            <DataGrid.Columns>
                <DataGridTemplateColumn   Header="{StaticResource name}" Width="320">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                        <TextBox Text="{Binding Naziv, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="{Binding BackgroundColor}"

  IsReadOnly="{Binding Path=IsReadonly}"
////CANNOT SET ISWRITEALLOWED HERE, TRIED EVERYTHING
 IsEnabled="{Binding Path=IsWriteAllowed, RelativeSource={RelativeSource Self}}"/>
                    </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
            <DataGrid.ContextMenu>
                <ContextMenu>
// ISWRITEALLOWED WORKING PERFECTLY Here
                    <MenuItem Command="{Binding  Delete}"   IsEnabled="{Binding Path=IsWriteAllowed}"  Header="{StaticResource delete}"/>
                    <MenuItem ClipToBounds="True" Command="{Binding Add}"  Header="{StaticResource add}"/>
                </ContextMenu>

2 个答案:

答案 0 :(得分:1)

您提供的答案是正确的,但我想我会提供另一种选择。如果您使用的是.NET 4或更高版本,则可以使用x:Reference标记。

我可以看到你的根窗口名为DefinitionWin,所以你可以像这样使用x:Reference:

IsEnabled="{Binding DataContext.IsWriteAllowed, Source={x:Reference DefinitionWin}}"

这可以减轻WPF在视觉树上旅行的速度。

答案 1 :(得分:0)

好吧,我似乎找到了解决方案......我已经搜索过,然后再次搜索......

IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.IsWriteAllowed}"

https://stackoverflow.com/questions/1127933/wpf-databinding...