开发者

Silverlight styles for custom controls. Using the BasedOn property

I had been previously using the DefaultStyleKey setting to set my datagrid's style, but now i want to extend one style with the BasedOn property of another. So now I have two styles with the same Type, and I must be more specifc than simply setting the DefaultStyleKey.

Unfortunately, I can't seem to access the generic.xaml file to get at the style's I've created.

There 开发者_如何学编程must be a workaround for when you don't want to use DefaultStyleKey, and need to access your styles from the code


Are you looking for something like this:

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("/CustomControl;component/Themes/generic.xaml", UriKind.RelativeOrAbsolute);
Style style = rd["StyleKey"] as Style;
}

'CustomControl' is the name of your project, and StyleKey the Key for the style you want to access.


So why can't you extend your datagrid as well, create a new class and use default style key as new type and in your generic.xaml you can create a new style based on your previous style.

But in siverlight, we create one base style as named style and we inherit styles from it.

<style x:Key="BaseDataGrid"

<style TargetType="MyGrid1" 
       BasedOn="BaseDataGrid"

<style TargetType="MyGrid2" 
       BasedOn="BaseDataGrid"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜