none
控件引用到ContentPresenter中的问题 RRS feed

  • 问题

  • 需求如下:

    界面上有多个Path,点击某个Path后,把该Path显示在ContentPresenter中。

    代码如下:

    <UserControl x:Class="PathSelection.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Height="300" Width="400">
    
      <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
          <ColumnDefinition/>
          <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0">
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <Path Grid.Row="0" MouseLeftButtonDown="path_MouseLeftButtonDown" x:Name="p1" Data="F1 M 14.1105,45.049C 14.04,40.6248 12.9429,38.9392 12.8029,34.3056C 12.3164,18.0073 15.4413,31.0713 26.0573,26.9218C 36.0606,23.0116 31.2564,25.2721 35.1836,16.3038C 36.5344,13.2147 35.1741,10.0297 36.1564,6.73324C 37.8515,1.05941 40.5711,0.666649 46.3734,0.666649C 55.2922,0.666649 64.4058,0.735069 73.3246,0.735069C 76.3747,0.735069 78.8647,0.901451 81.8885,1.05624C 78.8079,9.56966 76.1694,14.8076 76.5379,23.6032C 76.7822,29.4582 75.8872,28.9339 80.9167,32.1156C 83.9111,34.0108 91.1485,41.9715 92.1055,44.7658C 96.4095,46.6676 105.263,47.4601 109.834,46.0753C 113.555,44.3665 115.94,39.3415 123.307,33.9098C 128.951,35.0532 129.013,46.7946 131.028,48.9004C 134.759,52.7981 146.597,54.4964 143.919,60.5461C 140.129,60.7535 138.224,62.0907 134.435,62.5244C 130.89,62.9287 124.578,61.3368 121.054,61.5495C 120.107,65.4524 108.728,79.586 107.592,72.3382C 100.86,68.7121 107.082,85.7285 105.752,88.8176C 102.463,96.4729 93.7912,90.7527 90.6347,86.8614C 88.5468,84.2892 87.2929,83.4163 87.7277,79.5523C 88.1636,75.6684 90.7652,75.4072 91.9718,72.1202C 93.9796,66.6401 89.8292,69.2669 87.3476,66.8075C 83.9848,63.4762 88.3815,62.596 88.1415,58.7036C 85.1324,55.2249 85.4567,65.2787 79.528,53.8415C 79.309,57.3728 79.0153,58.9984 80.3219,61.659C 81.5253,64.1079 84.7513,65.7093 82.4108,69.249C 81.6001,70.4798 79.3806,69.6228 78.3657,71.1737C 77.7771,72.076 77.7182,74.5155 77.9993,75.6578C 78.6563,78.3205 81.3474,79.6945 82.2539,82.4846C 84.9808,90.8728 72.3907,84.7378 75.8872,93.8218C 78.1772,99.7736 90.2588,98.3407 96.1601,98.6881C 95.0514,102.312 98.0026,115.071 95.3494,113.931C 93.0805,112.958 92.713,106.804 88.5363,105.987C 84.1227,105.12 84.0501,106.931 80.105,105.824C 75.5503,104.547 71.1609,110.393 66.6189,107.798C 64.5858,99.7326 74.4343,104.566 73.4541,97.39C 72.3254,89.1176 65.3628,97.2837 62.4285,98.0406C 62.3,95.4054 61.3198,95.6549 61.1303,93.0111C 58.2107,93.1754 57.7832,99.7831 57.8054,104.121C 56.2387,103.245 53.9624,101.62 51.9314,102.102C 51.4387,104.36 53.6792,104.187 54.1572,105.987C 54.5804,107.565 55.6828,109.28 56.5188,111.122C 58.953,116.474 53.6539,119.6 58.1465,120.917C 64.7416,122.855 84.4828,123.015 77.5107,137.609C 74.4743,137.899 75.1386,127.663 71.9958,128.042C 70.2913,131.205 70.4566,140.276 72.4065,144.502C 75.4124,146.123 75.3524,143.304 77.8908,145.327C 79.7186,146.785 80.3693,149.183 81.8896,150.828C 85.9126,155.174 94.0523,158.612 92.5077,169.473C 88.0309,171.255 86.5306,174.994 82.8624,178.316C 77.6497,183.035 68.096,184.883 60.7249,186.344C 54.731,187.533 51.1965,190.453 43.8981,183.888C 39.6382,180.057 32.1629,182.67 26.4869,188.26C 24.597,184.583 27.5598,177.992 23.4947,174.436C 21.7964,172.95 19.1464,170.65 19.6886,168.173C 35.67,166.638 28.1283,175.64 37.1282,176.372C 37.8052,174.007 35.6173,173.992 35.1836,172.316C 34.8414,170.989 37.3356,166.867 35.67,166.638C 28.5326,165.667 32.3745,160.476 28.3715,160.072C 27.9346,166.853 21.9733,161.192 20.7499,159.179C 18.6494,155.728 13.045,159.221 18.9695,150.537C 18.8821,145.926 9.60426,149.502 9.07362,143.286C 8.60193,137.789 16.2089,138.097 18.5652,136.404C 18.5367,136.195 18.5325,135.301 18.5652,135.098C 14.6369,134.114 12.3512,137.526 10.0454,133.556C 7.97653,129.989 12.3164,124.637 3.88195,121.716C -3.57864,115.555 3.9925,105.992 4.52946,99.661C 5.16013,92.2594 1.48985,87.8858 5.18118,82.6309C 6.55517,80.6747 11.5731,83.1342 12.4775,81.0106C 13.5872,78.4132 12.8081,71.0389 13.2798,68.2783C 15.0728,57.8202 20.7235,61.9401 27.3997,61.5495C 30.2604,58.2319 32.1755,52.5549 29.6676,48.2518C 25.6741,47.4348 12.7902,51.6294 14.1105,45.049 Z " Fill="Red"/>
          <Path Grid.Row="1" MouseLeftButtonDown="path_MouseLeftButtonDown" x:Name="p2" Data="F1 M 6.57698,81.9682C 6.57698,76.9892 6.72333,73.0136 5.99791,68.1862C 5.35882,63.9379 2.75509,61.8617 3.60475,54.7812C 6.97181,46.293 12.1919,53.7978 17.672,54.0821C 24.8568,54.4516 33.9209,50.5981 39.4063,46.6173C 43.6367,43.5482 49.8286,45.9539 51.2489,40.4633C 52.4534,35.8191 50.3329,34.7525 48.5704,31.695C 47.1364,29.2124 46.3457,25.6306 46.3783,22.7773C 46.4531,16.5496 51.6658,8.44888 57.5692,6.88326C 67.0513,4.37118 71.0764,2.15908 76.8692,2.16537C 83.0359,2.17068 81.6629,-0.269875 88.2202,1.04623C 91.7999,1.76424 91.3766,5.23031 94.5436,6.88326C 97.1495,8.24355 106.448,7.1086 105.285,10.8104C 104.535,13.1963 100.329,12.9709 100.778,16.1727C 103.007,20.3978 108.047,19.088 110.653,19.9651C 113.535,20.9327 112.958,24.5355 116.176,24.4998C 122.176,24.4334 128.985,17.2793 133.803,17.2392C 138.27,17.2035 147.008,17.852 143.367,25.2073C 142.207,27.5478 139.572,30.3211 137.622,31.9593C 134.852,34.2872 132.166,37.2921 129.213,39.3114C 125.747,41.6846 122.261,42.0468 118.71,44.0251C 115.677,45.7097 110.563,50.6919 109.304,53.5914C 108.023,56.5405 109.527,56.6079 108.391,59.338C 107.501,61.4752 106.08,62.5239 104.832,64.4454C 104.199,65.4203 104.165,66.6606 103.4,67.6334C 102.49,68.7906 101.254,70.5604 100.309,71.8165C 100.309,71.8165 98.8372,74.1843 99.013,77.0229C 99.3258,82.1029 102.212,86.2891 106.868,88.4559C 116.28,92.8348 111.904,97.0567 111.005,104.998C 110.507,109.391 114.923,114.476 115.371,117.59C 115.825,120.762 116.581,129.452 118.299,132.179C 116.597,132.91 117.36,128.549 113.553,128.84C 110.246,129.089 109.559,128.78 110.134,132.55C 112.677,132.67 112.08,132.179 114.492,132.407C 114.923,134.883 110.377,137.579 108.652,139.045C 106.086,141.229 104.95,141.876 101.52,141.702C 99.5753,138.962 102.816,137.277 102.692,135.179C 100.032,133.431 95.7681,132.076 92.4369,132.246C 91.9399,133.955 93.2402,136.591 94.87,136.625C 90.4533,136.577 83.8413,137.11 81.4966,133.12C 79.6109,129.915 78.0021,128.669 72.383,130.022C 71.6281,131.344 71.0911,133.863 72.3019,135.099C 73.8065,136.633 76.3734,134.876 77.8463,135.803C 81.5913,138.163 73.7823,141.419 71.685,141.656C 68.8749,141.975 68.8001,140.589 67.1345,139.055C 65.6468,137.684 64.8245,137.418 63.0009,136.38C 61.6038,135.583 60.3435,135.778 58.9453,134.691C 57.3292,133.438 56.2426,133.845 54.857,130.899C 53.7663,127.351 52.505,122.071 50.2718,119.175C 46.7374,114.585 45.2971,120.366 42.1027,122.953C 40.0485,124.614 36.7668,128.958 32.3258,130.079C 28.3597,131.08 19.8978,129.802 15.9464,127.647C 13.07,123.115 12.3056,120.9 6.87178,116.765C 3.84164,114.459 2.17391,114.824 0.95575,111.22C 0.117668,108.739 1.38531,103.229 1.21686,100.522C 0.885208,95.2364 3.09517,94.4151 4.69025,89.6056C 6.34641,84.6098 6.91074,81.5986 6.91074,75.951C 6.91074,74.3496 6.51276,71.9344 6.48328,69.6433" Fill="Blue"/>
        </Grid>
        <Grid Grid.Column="1">
          <ContentPresenter x:Name="Presenter" Margin="5"/>
        </Grid>
      </Grid>
    </UserControl>


    后台代码:

    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Shapes;
    
    namespace PathSelection
    {
      public partial class MainPage : UserControl
      {
        public MainPage()
        {
          InitializeComponent();
        }
    
        private void path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          Presenter.Content = sender as UIElement;
        }
      }
    }
    
    

     

    出现的问题:

    点击Path后,异常:ArgumentException-值不在预期的范围内

    2011年9月1日 3:25

答案

  • 你好,

    这是因为当前点击的Path已经包含在了一个Grid,所以它不能再是其它UI控件的子控件。

    首先将左边包含Path的Grid命名为 "myGrid",然后在MouseLeftButtonDown事件处理程序:

            private void path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                Path p = sender as Path;

                myGrid.Children.Remove(p);

                Presenter.Content = p;
            }
    这样做虽然可以在ContentPresenter控件里看到Path,但myGrid里的却不见了。

    所以解决方案是创建一个新的Path(基于当前点击的Path)再赋值给 Presenter.Content 。


    Daoping Liu - MSFT
    2011年9月6日 10:44
    版主

全部回复

  • 求解啊,谢谢各位
    2011年9月5日 1:19
  • 你好,

    这是因为当前点击的Path已经包含在了一个Grid,所以它不能再是其它UI控件的子控件。

    首先将左边包含Path的Grid命名为 "myGrid",然后在MouseLeftButtonDown事件处理程序:

            private void path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                Path p = sender as Path;

                myGrid.Children.Remove(p);

                Presenter.Content = p;
            }
    这样做虽然可以在ContentPresenter控件里看到Path,但myGrid里的却不见了。

    所以解决方案是创建一个新的Path(基于当前点击的Path)再赋值给 Presenter.Content 。


    Daoping Liu - MSFT
    2011年9月6日 10:44
    版主