1

我正在尝试将 SIDJavnaUstanovaViewModel 的属性 IsWriteAllowed 设置为 TextBox。我不能这样做,我尝试了相对源元素名称,但它不起作用。我正在尝试将 IsWriteAllowed 属性设置为 TextBox IsEnabled 属性。在我有 StaticResource 的 XAML-s 中,这工作正常,但是在这里,当我以编程方式设置 DataContext 时,我无法绑定它。我以编程方式设置了 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>
4

2 回答 2

1

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

我可以看到您的根窗口已命名DefinitionWin,因此您可以像这样使用 x:Reference:

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

这减轻了 WPF 必须遍历可视化树。

于 2013-09-02T10:28:37.973 回答
0

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

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

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

于 2013-09-02T09:27:32.590 回答