为什么UserControl不使用绑定和x:名称?

时间:2015-07-10 15:25:34

标签: c# wpf binding

我想使用UserControl,例如ContentControl。

例如,如果CheckButton Check,则更改按钮的isEnalbe属性。

但是,不要执行。

为什么??????

显示附加代码!

== Window.xaml ==

<?php

if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
    // Get the data like you would with traditional post
    $rawImage=$GLOBALS['HTTP_RAW_POST_DATA'];

    // Remove the headers  
    $removeHeaders=substr($rawImage, strpos($rawImage, ",")+1);

    // decode it from base 64 and into image data only
    $decode=base64_decode($removeHeaders);

    // save to your server
    $saveName = 'C:\Users\Administrator\Downloads\image009.png';
    $fopen = fopen($saveName, 'wb' );
    fwrite( $fopen, $decode);
    fclose( $fopen );
}

?>

== UserControl.xaml ==

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4" x:Class="WpfApplication4.MainWindow"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <local:UserControl1 >
      <local:UserControl1.Buttons>
        <Button IsEnabled="{Binding Path=IsChecked, ElementName=CheckBox}" Height="50"/>
        <Button Height="50"/>
      </local:UserControl1.Buttons>
    </local:UserControl1>
    <CheckBox x:Name="CheckBox"/>
  </Grid>
</Window>

== UserControl.xaml.cs ==

<UserControl x:Class="WpfApplication4.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
  <StackPanel x:Name="MainStack">
  </StackPanel>
</UserControl>

1 个答案:

答案 0 :(得分:0)

您的Button嵌套在自定义控件中,并且无法直接访问CheckBox。添加属性到wchich CheckBox绑定其IsChecked属性

   <CheckBox x:Name="CheckBox" IsChecked="{Binding IsChecked}"/>

然后您可以按如下方式引用CheckBox的DataContext

  <Button IsEnabled="{Binding RelativeSource={RelativeSource AncestorType=Grid} Path=DataContext.IsChecked}" Height="50"/>