开发者

WP7: How to override static resources?

开发者 https://www.devze.com 2023-02-18 19:51 出处:网络
How do I override static resources in Windows Phone 7 Apps? I am stacking many TextBoxes in a list and I want to make the margin between them smaller, but without retemplating the entire control.

How do I override static resources in Windows Phone 7 Apps?

I am stacking many TextBoxes in a list and I want to make the margin between them smaller, but without retemplating the entire control.

I figured PhoneTouchTargetOverhang is whats causing the large margin. How do I override it?

I have tried adding this to my PhoneApplicationPage.Resources:

<Thickness x:Key="PhoneTouchTargetOverhang">0</Thickness>

... but it is not affecting anything.

A开发者_如何学Pythonlso I'm trying to override other resources such as default background and foreground colors without success. Whatever I do, nothing happens.

Clarification:

  • I don't want to re-template the entire control, only change some resources used by the template.
  • I don't care if its "bad design" to remove the touch margins on the textbox-template. For the case I'm working on its a requirement to have them close together...


I haven't tried this out but setting the Top and Bottom margins to negative numbers should move the TextBoxes closer together.

<TextBox Margin="0,-10,0,-10" Text="Some text"/>


I might have misunderstood...

But if you're just repeating the TextBoxes inside a databound ListBox then you I think could just set the Margin for these TextBoxes inside the DataTemplate.

<ListBox ItemsSource="{...}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Margin="0" Text="{...}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


You should check out these resources when trying to change static resources:

How to: Apply Theme Resources for Windows Phone

and also

Resources Overview - This actually shows how to style a textbox in xaml (in your case the textbox control). Since your working in wp7 you can declare these in the app.xaml file. This is a nice approach if you wish to apply these styles throughout the app. If not Praetorian's solution is an alternative for specific controls you would like to style.

From what I've used there is no reason why this shouldn't work. And honestly in my opinion it's better (neater) to create a template than start applying negative margins on individual controls in your xaml files.

Be aware also that there is a reason for the margins for user experience purposes. Put them too close and it makes it difficult to select a certain control. I believe there is a requirement for in the design guidelines but I'm not sure exactly.

Here is an example of what to put in app.xaml:

<Style x:Key="TextBoxStyle1" TargetType="TextBox">  
    <Setter Property="Template">  
         <Setter.Value> 
              <ControlTemplate TargetType="TextBox">
              </ControlTemplate>
         </Setter.Value>
    </Setter>
</Style>

Here is full example on creating a template for the textbox control, if you don't want to start from scratch.

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消