关于WPF xaml中的数据模板或样式的问题 [英] Question about Data Template or Style in WPF xaml

查看:118
本文介绍了关于WPF xaml中的数据模板或样式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,具有以下简单的XAML(不需要阅读 - 仅供参考):

I have a textbox that has the following simple XAML (not necessary to read it - just have it for reference):

<TextBox Name="m_ctrlUserDeviceType" Style="{StaticResource textStyleTextBox}" Text="{Binding Source={x:Static api:MySettings.Instance}, Path=UserDeviceType, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Validation.Error="TextBox_Error" MinHeight="25" Margin="4" VerticalAlignment="Top" MaxLength="23" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100"></TextBox>

为了完整,styleStyleTextBox样式看起来像这样(再次,不需要阅读来回答问题):

For completeness, the style textStyleTextBox looks like this (again, not necessary to read to answer question):

<Style x:Key="textStyleTextBox" TargetType="TextBox">
    <Setter Property="Foreground" Value="#333333" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="MinHeight" Value="2" />
        <Setter Property="MinWidth" Value="100" />
        <Setter Property="Margin" Value="4" />
        <Setter Property="MaxLength" Value="23" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Left" />
       <!-- <Setter Property="Binding Source" Value="{x:Static api:MySettings.Instance}"/>
        <Setter Property="Binding ValidatesOnExceptions" Value="true" />
        <Setter Property="Binding NotifyOnValidationError" Value="true" /> -->



  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
                    Path=(Validation.Errors)[0].ErrorContent}" />
    </Trigger>
  </Style.Triggers>
</Style>

我有很多的东西(MiHeight,Margin等)的风格,因为我有很多这些文本框和它们几乎完全一样。事实上,还有更多的共同点,而不仅仅是风格。绑定到MySettings类的细节几乎是一样的。唯一的区别是文本框绑定的特定属性。另外,我总是使用TextBox_Error for Validation.Error。

I have a lot of the stuff (MiHeight, Margin, etc.)in the style because I have a lot of these textboxes and they're almost exactly the same. In fact, there's a lot more in common than just the style. The details of the binding to the class MySettings are almost the same. The only difference is which particular property the textbox is binding too. Additionally, I always user TextBox_Error for Validation.Error.

有没有办法把绑定信息放在样式或数据模板中,所以我不必继续为每个文本框打字?

我需要为每个文本框分配一个单独的属性(Path),我想我还需要对于将来添加的某些特定文本框(与MySettings中的数据绑定无关),不能使用它们的任何功能。

I would need to be able to assign an individual property (Path) for each textbox, and I suppose I still need the ability to not use any of it for some particular textbox added in the future (that has nothing to do with databinding to MySettings).

有没有办法把TextBox_Error部分放在style或DataTemplate里面?使用Setter属性似乎不适合我。

我不断提到数据模板,因为我认为答案可能与阅读有关Pro Silverlight 2在C#2008中。但是,我无法弄清楚。我也尝试向Style添加内容,您可以从该部分的注释出来的内容中看到。

I keep mentioning Data Template as I think the answer might have something to do with that based on reading Pro Silverlight 2 in C# 2008. However, I wasn't able to figure it out. I also tried adding stuff to "Style" as you can see from the commented out stuff in that section.

谢谢,

Dave

推荐答案

我不认为有办法做你所要求的。但是,我认为你可以用不同的方法来实现。

I dont think that there is a way to do what you are asking. However, I do think that you could go about it a different way.

我将研究一下,正在创建一个扩展 TextBox的自定义控件,然后创建一些依赖属性,当控件初始化时,设置绑定和错误验证。

What I would look into, is creating a custom control that extends TextBox, then create some dependency properties that, when the control is initialised, setup the bindings and error validation.

这样,您可以使用自定义文本框遍布您的应用程序并控制每个属性,甚至使它们的风格相同(只需更改您的样式的目标类型)

This way you can use your custom textbox all over your app and control every property, and even style them the same (just change the target type of your style)

HTH

这篇关于关于WPF xaml中的数据模板或样式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆