如何在WPF组合框中显示路径元素(图标)?

时间:2015-03-26 15:19:42

标签: c# wpf combobox

我只能在按钮元素上看到路径:

<ComboBox
    Margin="0 0 0 0"
    ItemsSource="{Binding Path=SearchTypes}"
    SelectedItem="{Binding Path=SearchType}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
             <Button>
                 <Path
                    Stretch="Uniform"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Data="{Binding Converter={StaticResource searchtypetoimagepathconverter}}" />
             </Button>
         </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

修改 我的转换器看起来像这样:

 public class SearchTypeToImagePathConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter,
         System.Globalization.CultureInfo culture)
     {
         if ((SearchType)value == SearchType.Customers)
             return App.Current.Resources["Geometry.User"];
         else
             return App.Current.Resources["Geometry.Share"];

     }

     public object ConvertBack(object value, Type targetType, object parameter,
         System.Globalization.CultureInfo culture)
     {
         return value.Equals(true) ? parameter : Binding.DoNothing;
     }
 }

编辑2

以下xaml也有效:

<ComboBox Grid.Column="0">
    <ComboBoxItem Name="share">                                  
      <Path Stretch="Uniform"
         Margin="0 0 0 0"
         VerticalAlignment="Center"
         Fill="{TemplateBinding Foreground}"
         Data="{StaticResource Geometry.Share}" />
    </ComboBoxItem>
    <ComboBoxItem Name="user">                        
       <Path Stretch="Uniform"                       
         Margin="0 0 0 0"
         VerticalAlignment="Center"
         Fill="{TemplateBinding Foreground}"
         Data="{StaticResource Geometry.User}" />
    </ComboBoxItem>
</ComboBox>

因此转换器的结果是返回StreamGeometry,它只显示在Button内的Path.Data中。我不知道这意味着什么。

path-element的正确父级是什么?

4 个答案:

答案 0 :(得分:1)

使用xaml图标的流行方法如下所示。所以回答你问题Path的父亲可能是一个VisualBrush。

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
      <Path x:Key="Repeat" Data="M33.6412,13.3342 C34.1892,13.3157 34.7319,13.5192 35.13,13.9098 L35.481,14.2528 C35.481,14.2528 35.582,14.3529 35.7709,14.5368 C35.9579,14.7128 36.23,15.0029 36.5609,15.4108 C37.1629,16.2189 37.9739,17.4969 38.4219,19.3309 C38.8089,21.1459 38.7448,23.518 37.6809,25.8689 C37.1779,27.055 36.3619,28.151 35.374,29.135 C34.385,30.122 33.173,30.941 31.8641,31.526 C30.5591,32.123 29.1672,32.514 27.7932,32.7191 C26.6272,32.835 25.4812,32.949 24.3702,33.058 C22.3023,33.225 20.3734,33.306 18.7194,33.3311 C18.2234,33.337 17.7625,33.3311 17.3184,33.322 L17.3184,37.4851 C17.3184,37.9501 17.0455,38.3731 16.6175,38.5631 C16.1905,38.7541 15.6875,38.6781 15.3385,38.3651 C11.6946,35.1181 8.44271,30.534 8.10575,29.838 C7.94676,29.513 7.94773,29.134 8.10672,28.81 C8.44674,28.111 11.7056,23.539 15.3385,20.3029 C15.5615,20.1049 15.8435,20.0009 16.1305,20.0009 C16.2945,20.0009 16.4615,20.0329 16.6175,20.1029 C17.0455,20.2929 17.3184,20.7169 17.3184,21.1819 L17.3184,25.4649 L18.7194,25.4649 L24.3702,25.4649 L25.9463,25.4649 L26.7262,25.4649 L27.2972,25.443 C28.0551,25.394 28.7772,25.2489 29.4311,25.015 C30.7441,24.555 31.7651,23.706 32.385,22.7049 C33.01,21.7019 33.2271,20.5769 33.1671,19.6069 C33.113,18.6339 32.807,17.8179 32.546,17.2659 C32.494,17.1589 32.447,17.0689 32.4051,16.9849 C32.3871,16.9309 32.2491,16.7249 32.1991,16.6379 C32.065,16.4209 31.995,16.3049 31.995,16.3049 C31.4251,15.3779 31.7301,14.1728 32.679,13.6148 C32.9817,13.4363 33.3124,13.3453 33.6412,13.3342 z M22.5228,8.9E-05 C22.8046,0.003539 23.0829,0.107522 23.301,0.302508 C26.9451,3.5505 30.1972,8.13145 30.5352,8.83045 C30.6932,9.15446 30.6922,9.53443 30.5332,9.85644 C30.1932,10.5554 26.9351,15.1274 23.301,18.3644 C23.079,18.5624 22.796,18.6674 22.509,18.6674 C22.345,18.6674 22.179,18.6324 22.024,18.5634 C21.5949,18.3754 21.32,17.9504 21.32,17.4844 L21.32,13.2024 L19.9199,13.2024 L14.2688,13.2024 L12.6927,13.2024 L11.9117,13.2034 L11.3407,13.2234 C10.5837,13.2744 9.86266,13.4184 9.20768,13.6524 C7.89466,14.1114 6.8726,14.9604 6.25461,15.9604 C5.62861,16.9634 5.41059,18.0914 5.47059,19.0594 C5.52461,20.0343 5.83058,20.8484 6.09158,21.4004 C6.14462,21.5073 6.19162,21.5993 6.23459,21.6823 C6.25058,21.7353 6.3906,21.9433 6.43961,22.0283 C6.57261,22.2464 6.64359,22.3623 6.64359,22.3623 C7.21465,23.2883 6.90764,24.4953 5.95962,25.0523 C5.15259,25.5273 4.14457,25.3833 3.50851,24.7583 L3.15651,24.4143 C3.15651,24.4143 3.05653,24.3153 2.8675,24.1303 C2.67951,23.9523 2.40753,23.6643 2.07848,23.2563 C1.4755,22.4473 0.664446,21.1703 0.216437,19.3354 C-0.169559,17.5204 -0.105531,15.1484 0.956445,12.7964 C1.45951,11.6114 2.27648,10.5154 3.26454,9.53144 C4.25456,8.54444 5.46461,7.72646 6.77561,7.13946 C8.07966,6.54446 9.47166,6.15448 10.8467,5.94647 C12.0118,5.83145 13.1588,5.71946 14.2688,5.61048 C16.3368,5.44346 18.2669,5.35947 19.9199,5.33549 C20.416,5.33146 20.877,5.33549 21.32,5.34348 L21.32,1.18251 C21.32,0.716507 21.5949,0.292529 22.024,0.104512 C22.1833,0.032141 22.3537,-0.001981 22.5228,8.9E-05 z" Fill="Black" Stretch="Uniform" UseLayoutRounding="False"/>
      <Path x:Key="Tablet" Data="M9.3486,21.052 C12.3243,21.076 14.586,23.011 14.586,26.307 L11.8403,26.307 C10.4715,26.307 9.3486,25.232 9.3486,23.859 C9.3486,23.859 9.3226,21.276 9.3486,21.052 z M35.334,15.289 C33.91,15.289 32.752,16.442 32.752,17.866 C32.752,19.292 33.91,20.448 35.334,20.448 C36.755,20.448 37.915,19.292 37.915,17.866 C37.915,16.442 36.755,15.289 35.334,15.289 z M9.51959,15.129 C11.4734,15.1341 14.5284,15.6521 17.266,18.4421 C19.2481,20.458 20.4901,23.01 20.4981,26.307 L17.4221,26.307 C17.415,24.327 16.524,22.117 15.091,20.657 C12.894,18.422 10.628,18.245 9.334,18.258 L9.334,15.1301 C9.39468,15.1292 9.45657,15.1289 9.51959,15.129 z M9.33399,9.19698 C11.922,9.19698 17.076,9.73195 21.519,14.2597 C24.555,17.3515 26.451,21.6913 26.47,26.306 L23.246,26.306 C23.227,22.3512 21.683,19.0074 19.198,16.4726 C15.56,12.7758 11.276,12.3308 9.33399,12.3308 z M20.055,5.01599 C15.143,5.01599 10.23,5.297 5.33398,5.75299 L5.33398,29.984 C10.23,30.44 15.143,30.718 20.055,30.718 C23.594,30.718 27.134,30.544 30.667,30.309 L30.667,5.42599 C27.134,5.19 23.594,5.01599 20.055,5.01599 z M20,0 C25.076,0 30.476,0.242004 35.531,0.718018 C38.066,0.958008 40,3.086 40,5.63 L40,30.104 C40,32.648 38.066,34.776 35.531,35.016 C30.476,35.494 25.076,35.737 20,35.737 C14.924,35.737 9.522,35.494 4.47,35.016 C1.935,34.776 0,32.648 0,30.104 L0,5.63 C0,3.086 1.935,0.958008 4.47,0.718018 C9.522,0.242004 14.924,0 20,0 z" Fill="Black" Stretch="Uniform" UseLayoutRounding="False"/>
  </Page.Resources>
  <Grid>  
  <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
  </Grid.ColumnDefinitions>
    <Rectangle>
        <Rectangle.Fill>
            <VisualBrush Visual="{StaticResource Repeat}" />
        </Rectangle.Fill>
    </Rectangle>
     <Rectangle Fill="Black" Grid.Column="1">
        <Rectangle.OpacityMask>
            <VisualBrush Visual="{StaticResource Tablet}" />
        </Rectangle.OpacityMask>
    </Rectangle>
  </Grid>
</Page>

这表明了两种方式。用作不透明蒙版或用作填充。这可以应用于任何用于创建图标效果的控件。

举个例子

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
      <Path x:Key="Repeat" Data="M33.6412,13.3342 C34.1892,13.3157 34.7319,13.5192 35.13,13.9098 L35.481,14.2528 C35.481,14.2528 35.582,14.3529 35.7709,14.5368 C35.9579,14.7128 36.23,15.0029 36.5609,15.4108 C37.1629,16.2189 37.9739,17.4969 38.4219,19.3309 C38.8089,21.1459 38.7448,23.518 37.6809,25.8689 C37.1779,27.055 36.3619,28.151 35.374,29.135 C34.385,30.122 33.173,30.941 31.8641,31.526 C30.5591,32.123 29.1672,32.514 27.7932,32.7191 C26.6272,32.835 25.4812,32.949 24.3702,33.058 C22.3023,33.225 20.3734,33.306 18.7194,33.3311 C18.2234,33.337 17.7625,33.3311 17.3184,33.322 L17.3184,37.4851 C17.3184,37.9501 17.0455,38.3731 16.6175,38.5631 C16.1905,38.7541 15.6875,38.6781 15.3385,38.3651 C11.6946,35.1181 8.44271,30.534 8.10575,29.838 C7.94676,29.513 7.94773,29.134 8.10672,28.81 C8.44674,28.111 11.7056,23.539 15.3385,20.3029 C15.5615,20.1049 15.8435,20.0009 16.1305,20.0009 C16.2945,20.0009 16.4615,20.0329 16.6175,20.1029 C17.0455,20.2929 17.3184,20.7169 17.3184,21.1819 L17.3184,25.4649 L18.7194,25.4649 L24.3702,25.4649 L25.9463,25.4649 L26.7262,25.4649 L27.2972,25.443 C28.0551,25.394 28.7772,25.2489 29.4311,25.015 C30.7441,24.555 31.7651,23.706 32.385,22.7049 C33.01,21.7019 33.2271,20.5769 33.1671,19.6069 C33.113,18.6339 32.807,17.8179 32.546,17.2659 C32.494,17.1589 32.447,17.0689 32.4051,16.9849 C32.3871,16.9309 32.2491,16.7249 32.1991,16.6379 C32.065,16.4209 31.995,16.3049 31.995,16.3049 C31.4251,15.3779 31.7301,14.1728 32.679,13.6148 C32.9817,13.4363 33.3124,13.3453 33.6412,13.3342 z M22.5228,8.9E-05 C22.8046,0.003539 23.0829,0.107522 23.301,0.302508 C26.9451,3.5505 30.1972,8.13145 30.5352,8.83045 C30.6932,9.15446 30.6922,9.53443 30.5332,9.85644 C30.1932,10.5554 26.9351,15.1274 23.301,18.3644 C23.079,18.5624 22.796,18.6674 22.509,18.6674 C22.345,18.6674 22.179,18.6324 22.024,18.5634 C21.5949,18.3754 21.32,17.9504 21.32,17.4844 L21.32,13.2024 L19.9199,13.2024 L14.2688,13.2024 L12.6927,13.2024 L11.9117,13.2034 L11.3407,13.2234 C10.5837,13.2744 9.86266,13.4184 9.20768,13.6524 C7.89466,14.1114 6.8726,14.9604 6.25461,15.9604 C5.62861,16.9634 5.41059,18.0914 5.47059,19.0594 C5.52461,20.0343 5.83058,20.8484 6.09158,21.4004 C6.14462,21.5073 6.19162,21.5993 6.23459,21.6823 C6.25058,21.7353 6.3906,21.9433 6.43961,22.0283 C6.57261,22.2464 6.64359,22.3623 6.64359,22.3623 C7.21465,23.2883 6.90764,24.4953 5.95962,25.0523 C5.15259,25.5273 4.14457,25.3833 3.50851,24.7583 L3.15651,24.4143 C3.15651,24.4143 3.05653,24.3153 2.8675,24.1303 C2.67951,23.9523 2.40753,23.6643 2.07848,23.2563 C1.4755,22.4473 0.664446,21.1703 0.216437,19.3354 C-0.169559,17.5204 -0.105531,15.1484 0.956445,12.7964 C1.45951,11.6114 2.27648,10.5154 3.26454,9.53144 C4.25456,8.54444 5.46461,7.72646 6.77561,7.13946 C8.07966,6.54446 9.47166,6.15448 10.8467,5.94647 C12.0118,5.83145 13.1588,5.71946 14.2688,5.61048 C16.3368,5.44346 18.2669,5.35947 19.9199,5.33549 C20.416,5.33146 20.877,5.33549 21.32,5.34348 L21.32,1.18251 C21.32,0.716507 21.5949,0.292529 22.024,0.104512 C22.1833,0.032141 22.3537,-0.001981 22.5228,8.9E-05 z" Fill="Black" Stretch="Uniform" UseLayoutRounding="False"/>
      <Path x:Key="Tablet" Data="M9.3486,21.052 C12.3243,21.076 14.586,23.011 14.586,26.307 L11.8403,26.307 C10.4715,26.307 9.3486,25.232 9.3486,23.859 C9.3486,23.859 9.3226,21.276 9.3486,21.052 z M35.334,15.289 C33.91,15.289 32.752,16.442 32.752,17.866 C32.752,19.292 33.91,20.448 35.334,20.448 C36.755,20.448 37.915,19.292 37.915,17.866 C37.915,16.442 36.755,15.289 35.334,15.289 z M9.51959,15.129 C11.4734,15.1341 14.5284,15.6521 17.266,18.4421 C19.2481,20.458 20.4901,23.01 20.4981,26.307 L17.4221,26.307 C17.415,24.327 16.524,22.117 15.091,20.657 C12.894,18.422 10.628,18.245 9.334,18.258 L9.334,15.1301 C9.39468,15.1292 9.45657,15.1289 9.51959,15.129 z M9.33399,9.19698 C11.922,9.19698 17.076,9.73195 21.519,14.2597 C24.555,17.3515 26.451,21.6913 26.47,26.306 L23.246,26.306 C23.227,22.3512 21.683,19.0074 19.198,16.4726 C15.56,12.7758 11.276,12.3308 9.33399,12.3308 z M20.055,5.01599 C15.143,5.01599 10.23,5.297 5.33398,5.75299 L5.33398,29.984 C10.23,30.44 15.143,30.718 20.055,30.718 C23.594,30.718 27.134,30.544 30.667,30.309 L30.667,5.42599 C27.134,5.19 23.594,5.01599 20.055,5.01599 z M20,0 C25.076,0 30.476,0.242004 35.531,0.718018 C38.066,0.958008 40,3.086 40,5.63 L40,30.104 C40,32.648 38.066,34.776 35.531,35.016 C30.476,35.494 25.076,35.737 20,35.737 C14.924,35.737 9.522,35.494 4.47,35.016 C1.935,34.776 0,32.648 0,30.104 L0,5.63 C0,3.086 1.935,0.958008 4.47,0.718018 C9.522,0.242004 14.924,0 20,0 z" Fill="Black" Stretch="Uniform" UseLayoutRounding="False"/>
  </Page.Resources>
  <Grid>
  <ComboBox Grid.Column="0" Height="40" Width="200">
    <ComboBoxItem Name="share"> 
      <Rectangle Height="40" Width="40">
        <Rectangle.Fill>
            <VisualBrush Visual="{StaticResource Repeat}" />
        </Rectangle.Fill>
      </Rectangle>
    </ComboBoxItem>
    <ComboBoxItem Name="user"> 
      <Rectangle Fill="Black" Height="40" Width="40">
        <Rectangle.OpacityMask>
            <VisualBrush Visual="{StaticResource Tablet}" />
        </Rectangle.OpacityMask>
      </Rectangle>
    </ComboBoxItem>
   </ComboBox>
  </Grid>
</Page>

这是您的确切用例。如果这有助于他人的利益,请将其标记为答案。

答案 1 :(得分:0)

编辑你应该添加到Path元素:Stroke =&#34; Red&#34;并删除按钮。

答案 2 :(得分:0)

我同意评论说,Path对象不需要父母,但为了解决您的问题,这可能有所帮助:ButtonContentControl,增加了功能。您不需要添加的功能,因此请尝试将Path包裹在ContentControl

答案 3 :(得分:0)

Button具有默认样式,因此它提供了Stroke和Fill,使Button.Path.Data中指定的StreamGeometry可见。

然而,ContentControl或普通Path元素以某种方式获取这些不可见的值,因此它们不会显示。

这是不好的答案,但我已经多次编辑了这个问题,我在这里写了答案。请用合适的参考资料和数据做出更好的答案,我接受它。