优化 <Style TargetType="{x:Type local:CustomColorPicker}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomColorPicker}"> <StackPanel> <TextBlock Text="Available Colors:"/> <ListBox ItemsSource="{TemplateBinding AvailableColors}" SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedColor}"/> <TextBlock Text="Hex:"/> <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Hex, Mode=TwoWay}"/> <TextBlock Text="RGB:"/> <StackPanel Orientation="Horizontal"> <TextBox Width="40" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=R, Mode=TwoWay}"/> <TextBox Width="40" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=G, Mode=TwoWay}"/> <TextBox Width="40" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=B, Mode=TwoWay}"/> </StackPanel> <Rectangle Width="50" Height="50" Margin="5" Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedColor}"/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter></Style> 使之有好看的排版和样式
时间: 2024-02-14 08:04:24 浏览: 93
可以尝试将该段代码进行格式化和缩进,使其更加易读和清晰。另外,可以添加注释来帮助理解模板的不同部分的作用。以下是优化后的代码:
```xml
<Style TargetType="{x:Type local:CustomColorPicker}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomColorPicker}">
<StackPanel>
<!-- 显示可用颜色 -->
<TextBlock Text="Available Colors:"/>
<ListBox ItemsSource="{TemplateBinding AvailableColors}"
SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=SelectedColor}"/>
<!-- 显示 Hex 值 -->
<TextBlock Text="Hex:"/>
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Hex, Mode=TwoWay}"/>
<!-- 显示 RGB 值 -->
<TextBlock Text="RGB:"/>
<StackPanel Orientation="Horizontal">
<TextBox Width="40" Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=R, Mode=TwoWay}"/>
<TextBox Width="40" Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=G, Mode=TwoWay}"/>
<TextBox Width="40" Text="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=B, Mode=TwoWay}"/>
</StackPanel>
<!-- 显示选定颜色的矩形 -->
<Rectangle Width="50" Height="50" Margin="5"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=SelectedColor}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
阅读全文